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