Skip to main content

whisker_linux/
lib.rs

1//! Native GTK4 Linux Host for Whisker.
2//!
3//! Each scene node is a GTK widget placed with Rust-computed geometry and
4//! painted through `gtk::Snapshot`; text is measured and drawn with Pango.
5
6#![cfg(target_os = "linux")]
7#![warn(missing_docs)]
8
9use std::error::Error;
10use std::fmt;
11
12use whisker::{Element, ElementModuleDefinition};
13
14pub use app::request_view_frame;
15pub use gtk4;
16pub use module::{LinuxEventEmitter, LinuxModuleDefinition, LinuxView, LinuxViewDefinition};
17
18mod app;
19mod color;
20mod cursor;
21mod module;
22mod node;
23mod paint;
24mod scene;
25mod text;
26
27#[cfg(all(test, feature = "host-conformance"))]
28#[path = "../tests/host_conformance/mod.rs"]
29mod host_conformance_tests;
30
31/// Configuration for one standalone Linux window.
32#[derive(Clone, Debug)]
33pub struct LinuxAppConfig {
34    title: String,
35    width: i32,
36    height: i32,
37    background_rgb: [u8; 3],
38    modules: Vec<LinuxModuleDefinition>,
39}
40
41impl LinuxAppConfig {
42    /// Creates the default window configuration.
43    pub fn new(title: impl Into<String>) -> Self {
44        Self {
45            title: title.into(),
46            width: 1024,
47            height: 720,
48            background_rgb: [255, 255, 255],
49            modules: Vec::new(),
50        }
51    }
52
53    /// Sets the window background shown behind Whisker content.
54    pub fn with_background_rgb(mut self, red: u8, green: u8, blue: u8) -> Self {
55        self.background_rgb = [red, green, blue];
56        self
57    }
58
59    /// Installs one module's element schema and Host implementation.
60    pub fn with_module(
61        mut self,
62        _elements: ElementModuleDefinition,
63        host: impl Into<LinuxModuleDefinition>,
64    ) -> Self {
65        self.modules.push(host.into());
66        self
67    }
68}
69
70/// Failure while creating or running the GTK Host.
71#[derive(Debug)]
72pub struct LinuxError(pub(crate) String);
73
74impl fmt::Display for LinuxError {
75    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
76        formatter.write_str(&self.0)
77    }
78}
79
80impl Error for LinuxError {}
81
82/// Runs a Whisker application in a GTK window until the window closes.
83pub fn run(config: LinuxAppConfig, application: fn() -> Element) -> Result<(), LinuxError> {
84    run_with_application_hash(config, application, || 0)
85}
86
87/// Runs a Whisker application; Hot Reload remounts the root when the hash changes.
88pub fn run_with_application_hash(
89    config: LinuxAppConfig,
90    application: fn() -> Element,
91    application_hash: fn() -> u64,
92) -> Result<(), LinuxError> {
93    app::run_application(config, application, application_hash)
94}