Skip to main content

rdesktop_core/
renderer.rs

1use crate::config::{RendererConfig, RendererKind as ConfigRendererKind, WindowConfig};
2use crate::ipc::IpcHandler;
3use crate::window::WindowHandle;
4
5/// The kind of renderer being used.
6#[derive(Debug, Clone, Copy, PartialEq, Eq)]
7pub enum RendererKind {
8    /// System WebView (WebView2/WebKit)
9    WebView,
10    /// Chrome Embedded (CDP)
11    Chrome,
12}
13
14impl From<&ConfigRendererKind> for RendererKind {
15    fn from(kind: &ConfigRendererKind) -> Self {
16        match kind {
17            ConfigRendererKind::WebView => Self::WebView,
18            ConfigRendererKind::Chrome => Self::Chrome,
19        }
20    }
21}
22
23impl From<&RendererConfig> for RendererKind {
24    fn from(config: &RendererConfig) -> Self {
25        Self::from(&config.kind)
26    }
27}
28
29/// Core trait that both WebView and Chrome backends must implement.
30///
31/// This provides a unified API for creating windows, loading content,
32/// executing JavaScript, and handling IPC regardless of the underlying
33/// rendering engine.
34pub trait Renderer {
35    /// Initialize the renderer.
36    fn init(&mut self) -> crate::Result<()>;
37
38    /// Create a new window with the given configuration.
39    fn create_window(&mut self, config: &WindowConfig) -> crate::Result<WindowHandle>;
40
41    /// Load a URL in the specified window.
42    fn load_url(&self, window: WindowHandle, url: &str) -> crate::Result<()>;
43
44    /// Load HTML content directly.
45    fn load_html(&self, window: WindowHandle, html: &str) -> crate::Result<()>;
46
47    /// Execute JavaScript in the specified window.
48    fn eval_script(&self, window: WindowHandle, script: &str) -> crate::Result<()>;
49
50    /// Set the IPC handler for messages from the frontend.
51    fn set_ipc_handler(&mut self, handler: Box<dyn IpcHandler>);
52
53    /// Send a message to the frontend JavaScript.
54    fn send_to_frontend(&self, window: WindowHandle, message: &str) -> crate::Result<()>;
55
56    /// Set the window title.
57    fn set_title(&self, window: WindowHandle, title: &str) -> crate::Result<()>;
58
59    /// Set the window size.
60    fn set_size(&self, window: WindowHandle, width: u32, height: u32) -> crate::Result<()>;
61
62    /// Set whether the window is resizable.
63    fn set_resizable(&self, window: WindowHandle, resizable: bool) -> crate::Result<()>;
64
65    /// Show or hide the window.
66    fn set_visible(&self, window: WindowHandle, visible: bool) -> crate::Result<()>;
67
68    /// Close a window.
69    fn close_window(&mut self, window: WindowHandle) -> crate::Result<()>;
70
71    // ── Frameless / Custom Title Bar ────────────────────────────────
72
73    /// Minimize the window.
74    fn minimize_window(&self, window: WindowHandle) -> crate::Result<()>;
75
76    /// Toggle maximize/restore.
77    fn maximize_window(&self, window: WindowHandle) -> crate::Result<()>;
78
79    /// Check whether the window is currently maximized.
80    fn is_maximized(&self, window: WindowHandle) -> crate::Result<bool>;
81
82    /// Toggle fullscreen mode.
83    fn set_fullscreen(&self, window: WindowHandle, fullscreen: bool) -> crate::Result<()>;
84
85    /// Check whether the window is currently fullscreen.
86    fn is_fullscreen(&self, window: WindowHandle) -> crate::Result<bool>;
87
88    /// Begin an interactive window drag.
89    ///
90    /// Call this from a `mousedown` handler on a custom title bar element
91    /// to allow the user to drag the window from any region.
92    fn start_drag(&self, window: WindowHandle) -> crate::Result<()>;
93
94    /// Begin an interactive window resize.
95    ///
96    /// `edge` specifies which edge/corner to resize from.
97    fn start_resize(&self, window: WindowHandle, edge: ResizeEdge) -> crate::Result<()>;
98
99    /// Set whether the window has OS decorations (title bar + borders).
100    fn set_decorations(&self, window: WindowHandle, decorations: bool) -> crate::Result<()>;
101
102    /// Set the window's always-on-top state.
103    fn set_always_on_top(&self, window: WindowHandle, always: bool) -> crate::Result<()>;
104
105    /// Enable or disable click-through: when enabled, pointer events fall
106    /// through the window to whatever is behind it (used by wallpaper and
107    /// click-through overlays). Applied at window creation by default; this
108    /// method allows toggling it at runtime where the platform supports it.
109    ///
110    /// Default implementation is a no-op; backends override it to call the
111    /// platform-specific window API.
112    fn set_click_through(&self, _window: WindowHandle, _enabled: bool) -> crate::Result<()> {
113        Ok(())
114    }
115
116    // ── Lifecycle ───────────────────────────────────────────────────
117
118    /// Run the main event loop. This blocks until the application exits.
119    fn run(self: Box<Self>) -> crate::Result<()>;
120
121    /// Get the renderer kind.
122    fn kind(&self) -> RendererKind;
123}
124
125/// Edge or corner for interactive resize.
126#[derive(Debug, Clone, Copy, PartialEq, Eq)]
127pub enum ResizeEdge {
128    Top,
129    Bottom,
130    Left,
131    Right,
132    TopLeft,
133    TopRight,
134    BottomLeft,
135    BottomRight,
136}