refactor: move slint component into window state
parent
9a5817fc9a
commit
1e9b7f0e0e
|
@ -1,32 +1,24 @@
|
|||
use anyhow::Result;
|
||||
use slint::{
|
||||
platform::{Platform, WindowAdapter},
|
||||
PlatformError,
|
||||
};
|
||||
use std::rc::{Rc, Weak};
|
||||
use std::rc::Rc;
|
||||
|
||||
use super::femtovg_window::FemtoVGWindow;
|
||||
|
||||
pub struct CustomSlintPlatform {
|
||||
window: Weak<FemtoVGWindow>,
|
||||
window: Rc<FemtoVGWindow>,
|
||||
}
|
||||
|
||||
impl CustomSlintPlatform {
|
||||
pub fn new(window: &Rc<FemtoVGWindow>) -> Self {
|
||||
Self {
|
||||
window: Rc::downgrade(window),
|
||||
}
|
||||
pub fn new(window: Rc<FemtoVGWindow>) -> Self {
|
||||
Self { window }
|
||||
}
|
||||
}
|
||||
|
||||
impl Platform for CustomSlintPlatform {
|
||||
fn create_window_adapter(&self) -> Result<Rc<dyn WindowAdapter>, PlatformError> {
|
||||
self.window
|
||||
.upgrade()
|
||||
.map(|window| -> Rc<dyn WindowAdapter> { window })
|
||||
.ok_or_else(|| {
|
||||
PlatformError::Other(
|
||||
"Failed to create window adapter: window no longer exists".into(),
|
||||
)
|
||||
})
|
||||
fn create_window_adapter(&self) -> Result<Rc<(dyn WindowAdapter + 'static)>, PlatformError> {
|
||||
Result::Ok(Rc::clone(&self.window) as Rc<dyn WindowAdapter>)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -83,9 +83,9 @@ impl WindowingSystemBuilder {
|
|||
self
|
||||
}
|
||||
|
||||
pub fn build(self) -> Result<WindowingSystem> {
|
||||
pub fn build(&mut self) -> Result<WindowingSystem> {
|
||||
match self.config.component_definition {
|
||||
Some(_) => WindowingSystem::new(&self.config),
|
||||
Some(_) => WindowingSystem::new(&mut self.config),
|
||||
None => Err(anyhow::anyhow!("Slint component not set")),
|
||||
}
|
||||
}
|
||||
|
|
|
@ -35,12 +35,11 @@ pub struct WindowingSystem {
|
|||
state: WindowState,
|
||||
connection: Rc<Connection>,
|
||||
event_queue: EventQueue<WindowState>,
|
||||
component_instance: Rc<ComponentInstance>,
|
||||
event_loop: EventLoop<'static, WindowState>,
|
||||
}
|
||||
|
||||
impl WindowingSystem {
|
||||
fn new(config: &WindowConfig) -> Result<Self> {
|
||||
fn new(config: &mut WindowConfig) -> Result<Self> {
|
||||
info!("Initializing WindowingSystem");
|
||||
let connection = Rc::new(Connection::connect_to_env()?);
|
||||
|
||||
|
@ -65,7 +64,7 @@ impl WindowingSystem {
|
|||
Self::wait_for_configure(&mut event_queue, &mut state)?;
|
||||
let display = connection.display();
|
||||
|
||||
let component_instance = Self::initialize_renderer_and_ui(&mut state, &display, config)?;
|
||||
Self::initialize_renderer(&mut state, &display, config)?;
|
||||
|
||||
let event_loop = EventLoop::try_new().context("Failed to create event loop")?;
|
||||
|
||||
|
@ -73,7 +72,6 @@ impl WindowingSystem {
|
|||
state,
|
||||
connection,
|
||||
event_queue,
|
||||
component_instance,
|
||||
event_loop,
|
||||
})
|
||||
}
|
||||
|
@ -180,16 +178,12 @@ impl WindowingSystem {
|
|||
FemtoVGRenderer::new(context).context("Failed to create FemtoVGRenderer")
|
||||
}
|
||||
|
||||
fn initialize_renderer_and_ui(
|
||||
fn initialize_renderer(
|
||||
state: &mut WindowState,
|
||||
display: &WlDisplay,
|
||||
config: &WindowConfig,
|
||||
) -> Result<Rc<ComponentInstance>> {
|
||||
) -> Result<()> {
|
||||
let renderer = Self::create_renderer(state, display)?;
|
||||
let component_definition = config
|
||||
.component_definition
|
||||
.as_ref()
|
||||
.ok_or_else(|| anyhow::anyhow!("Component definition not set"))?;
|
||||
|
||||
let femtovg_window = FemtoVGWindow::new(renderer);
|
||||
let size = state.size();
|
||||
|
@ -198,21 +192,13 @@ impl WindowingSystem {
|
|||
femtovg_window.set_scale_factor(config.scale_factor);
|
||||
femtovg_window.set_position(LogicalPosition::new(0., 0.));
|
||||
|
||||
debug!("Setting up custom Slint platform");
|
||||
let platform = CustomSlintPlatform::new(&femtovg_window);
|
||||
slint::platform::set_platform(Box::new(platform))
|
||||
.map_err(|e| anyhow::anyhow!("Failed to set platform: {:?}", e))?;
|
||||
//debug!("Setting up custom Slint platform");
|
||||
|
||||
debug!("Creating Slint component instance");
|
||||
let slint_component: Rc<ComponentInstance> = Rc::new(component_definition.create()?);
|
||||
|
||||
slint_component
|
||||
.show()
|
||||
.map_err(|e| anyhow::anyhow!("Failed to show component: {:?}", e))?;
|
||||
|
||||
state.set_window(Rc::clone(&femtovg_window));
|
||||
|
||||
Ok(slint_component)
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn event_loop_handle(&self) -> LoopHandle<'static, WindowState> {
|
||||
|
@ -222,8 +208,13 @@ impl WindowingSystem {
|
|||
pub fn run(&mut self) -> Result<()> {
|
||||
info!("Starting WindowingSystem main loop");
|
||||
if let Some(window) = &self.state.window() {
|
||||
/*let platform = CustomSlintPlatform::new(window);
|
||||
slint::platform::set_platform(Box::new(platform))
|
||||
.map_err(|e| anyhow::anyhow!("Failed to set platform: {:?}", e))?;
|
||||
*/
|
||||
window.render_frame_if_dirty();
|
||||
}
|
||||
self.state.show_component();
|
||||
self.setup_wayland_event_source();
|
||||
|
||||
self.event_loop
|
||||
|
@ -256,8 +247,8 @@ impl WindowingSystem {
|
|||
);
|
||||
}
|
||||
|
||||
pub fn component_instance(&self) -> Rc<ComponentInstance> {
|
||||
Rc::clone(&self.component_instance)
|
||||
pub fn component_instance(&self) -> &ComponentInstance {
|
||||
self.state.component_instance()
|
||||
}
|
||||
|
||||
pub fn window(&self) -> Rc<FemtoVGWindow> {
|
||||
|
|
|
@ -1,14 +1,18 @@
|
|||
use std::rc::Rc;
|
||||
use std::{borrow::{Borrow, BorrowMut}, rc::Rc};
|
||||
use log::info;
|
||||
use slint::{LogicalPosition, PhysicalSize};
|
||||
use slint::{LogicalPosition, PhysicalSize, ComponentHandle};
|
||||
use slint_interpreter::{ComponentDefinition, ComponentInstance};
|
||||
use smithay_client_toolkit::reexports::protocols_wlr::layer_shell::v1::client::zwlr_layer_surface_v1::ZwlrLayerSurfaceV1;
|
||||
use wayland_client::protocol::{wl_pointer::WlPointer, wl_surface::WlSurface};
|
||||
use crate::rendering::femtovg_window::FemtoVGWindow;
|
||||
use crate::rendering::{femtovg_window::FemtoVGWindow, slint_platform::CustomSlintPlatform};
|
||||
use super::WindowConfig;
|
||||
use anyhow::Result;
|
||||
|
||||
pub mod dispatches;
|
||||
|
||||
pub struct WindowState {
|
||||
component_definition: ComponentDefinition,
|
||||
component_instance: Option<ComponentInstance>,
|
||||
surface: Option<Rc<WlSurface>>,
|
||||
layer_surface: Option<Rc<ZwlrLayerSurfaceV1>>,
|
||||
size: PhysicalSize,
|
||||
|
@ -22,7 +26,7 @@ pub struct WindowState {
|
|||
}
|
||||
|
||||
impl WindowState {
|
||||
pub fn new(config: &WindowConfig) -> Self {
|
||||
pub fn new(config: &mut WindowConfig) -> Self {
|
||||
Self {
|
||||
surface: None,
|
||||
layer_surface: None,
|
||||
|
@ -34,9 +38,24 @@ impl WindowState {
|
|||
scale_factor: config.scale_factor,
|
||||
height: config.height,
|
||||
exclusive_zone: config.exclusive_zone,
|
||||
component_definition: config.component_definition.take().unwrap(),
|
||||
component_instance: None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn show_component(&mut self) -> Result<()> {
|
||||
if let Some(window) = &self.window {
|
||||
let platform = CustomSlintPlatform::new(Rc::clone(window));
|
||||
slint::platform::set_platform(Box::new(platform))
|
||||
.map_err(|e| anyhow::anyhow!("Failed to set platform: {:?}", e))?;
|
||||
}
|
||||
|
||||
self.component_instance = Some(self.component_definition.create()?);
|
||||
|
||||
self.component_instance.as_ref().unwrap().show()?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn update_size(&mut self, width: u32, height: u32) {
|
||||
let new_size = PhysicalSize::new(width, height);
|
||||
if let Some(window) = &self.window() {
|
||||
|
@ -110,4 +129,8 @@ impl WindowState {
|
|||
pub fn set_pointer(&mut self, pointer: Rc<WlPointer>) {
|
||||
self.pointer = Some(pointer);
|
||||
}
|
||||
|
||||
pub fn component_instance(&self) -> &ComponentInstance {
|
||||
&self.component_instance.as_ref().unwrap()
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue