-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initializes WindowAdapter implementation (#1178)
- Loading branch information
1 parent
a000119
commit 0996ba7
Showing
6 changed files
with
111 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
use std::cell::Cell; | ||
use std::mem::transmute; | ||
use std::ptr::null; | ||
use winit::event_loop::ActiveEventLoop; | ||
|
||
/// Execution context of the runtime. | ||
pub struct RuntimeContext<'a> { | ||
el: &'a ActiveEventLoop, | ||
} | ||
|
||
impl<'a> RuntimeContext<'a> { | ||
pub(super) fn new(el: &'a ActiveEventLoop) -> Self { | ||
Self { el } | ||
} | ||
|
||
/// # Panics | ||
/// If called from the other thread than main thread. | ||
pub fn with<R>(f: impl FnOnce(&Self) -> R) -> R { | ||
let cx = CONTEXT.get(); | ||
assert!(!cx.is_null()); | ||
unsafe { f(&*cx) } | ||
} | ||
|
||
pub fn event_loop(&self) -> &ActiveEventLoop { | ||
self.el | ||
} | ||
|
||
pub(super) fn run(&self, f: impl FnOnce()) { | ||
assert!(CONTEXT.replace(unsafe { transmute(self) }).is_null()); | ||
f(); | ||
CONTEXT.set(null()); | ||
} | ||
} | ||
|
||
thread_local! { | ||
static CONTEXT: Cell<*const RuntimeContext<'static>> = Cell::new(null()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
use i_slint_renderer_skia::SkiaRenderer; | ||
use slint::platform::{Renderer, WindowAdapter}; | ||
use slint::PhysicalSize; | ||
use std::rc::Rc; | ||
|
||
/// Implementation of [`WindowAdapter`]. | ||
pub struct Window { | ||
winit: Rc<winit::window::Window>, | ||
slint: slint::Window, | ||
renderer: SkiaRenderer, | ||
} | ||
|
||
impl Window { | ||
pub fn new( | ||
winit: Rc<winit::window::Window>, | ||
slint: slint::Window, | ||
renderer: SkiaRenderer, | ||
) -> Self { | ||
Self { | ||
winit, | ||
slint, | ||
renderer, | ||
} | ||
} | ||
} | ||
|
||
impl WindowAdapter for Window { | ||
fn window(&self) -> &slint::Window { | ||
&self.slint | ||
} | ||
|
||
fn size(&self) -> PhysicalSize { | ||
let s = self.winit.inner_size(); | ||
|
||
PhysicalSize::new(s.width, s.height) | ||
} | ||
|
||
fn renderer(&self) -> &dyn Renderer { | ||
&self.renderer | ||
} | ||
} |