feat: add gtk_ui function for loading .ui

This commit is contained in:
Byson94
2025-11-07 20:22:56 +05:30
parent 26ee4e5560
commit e12d5f7fb9
7 changed files with 48 additions and 1 deletions

View File

@@ -5,6 +5,12 @@ All notable changes to `ewwii` are documented here.
This changelog follows the [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) format,
and this project adheres to [Semantic Versioning](https://semver.org/).
## [UNRELEASED]
## Added
- `gtk_ui` function for loading .ui files.
## [0.3.1] - 2025-11-01
## Fixed

2
Cargo.lock generated
View File

@@ -486,7 +486,7 @@ dependencies = [
[[package]]
name = "ewwii"
version = "0.3.0"
version = "0.3.1"
dependencies = [
"anyhow",
"bincode",

View File

@@ -56,6 +56,9 @@ fn build_gtk_widget_from_node(
WidgetNode::CircularProgress { props } => {
build_circular_progress_bar(props, widget_reg)?.upcast()
}
WidgetNode::GtkUI { props } => {
build_gtk_ui_file(props)?.upcast()
}
// WidgetNode::Graph { props } => build_graph(props, widget_reg)?.upcast(),
// WidgetNode::Transform { props } => build_transform(props, widget_reg)?.upcast(),
WidgetNode::Slider { props } => build_gtk_scale(props, widget_reg)?.upcast(),

View File

@@ -1971,6 +1971,25 @@ pub(super) fn build_gtk_combo_box_text(
Ok(gtk_widget)
}
pub(super) fn build_gtk_ui_file(
props: &Map,
) -> Result<gtk4::Widget> {
let path = get_string_prop(&props, "file", None)?;
let main_id = get_string_prop(&props, "id", None)?;
if !std::path::Path::new(&path).exists() {
return Err(anyhow::anyhow!("UI file not found: {}", path));
}
let builder = gtk4::Builder::from_file(&path);
let gtk_widget = builder
.object(&main_id)
.ok_or_else(|| anyhow::anyhow!("No widget with id '{}' in {}", main_id, path))?;
Ok(gtk_widget)
}
pub(super) fn build_gtk_expander(
props: &Map,
children: &Vec<WidgetNode>,

View File

@@ -32,7 +32,10 @@ pub enum WidgetNode {
Transform { props: Map },
EventBox { props: Map, children: Vec<WidgetNode> },
ToolTip { props: Map, children: Vec<WidgetNode> },
// Special
LocalBind { props: Map, children: Vec<WidgetNode> },
GtkUI { props: Map },
// Top-level macros
DefWindow { name: String, props: Map, node: Box<WidgetNode> },
@@ -166,6 +169,9 @@ pub fn get_id_to_widget_info<'a>(
get_id_to_widget_info(child, id_to_props, Some(id))?;
}
}
WidgetNode::GtkUI { props } => {
insert_wdgt_info(node, props, "GtkUI", &[], parent_id, id_to_props)?;
}
WidgetNode::ColorChooser { props } => {
// let id = hash_props_and_type(props, "ColorChooser");
insert_wdgt_info(node, props, "ColorChooser", &[], parent_id, id_to_props)?;

View File

@@ -84,6 +84,17 @@ pub fn register_all_widgets(
register_with_children!("tooltip", ToolTip);
register_with_children!("localbind", LocalBind);
// == Special widget
engine.register_fn(
"gtk_ui",
|path: &str, load: &str| -> Result<WidgetNode, Box<EvalAltResult>> {
let mut props = Map::new();
props.insert("file".into(), path.into());
props.insert("id".into(), load.into());
Ok(WidgetNode::GtkUI { props })
},
);
// == Special signal
let keep_signal_clone = keep_signal.clone();
engine.register_fn(

View File

@@ -106,6 +106,7 @@ impl WidgetNode {
| node @ WidgetNode::ColorChooser { props }
| node @ WidgetNode::CircularProgress { props }
| node @ WidgetNode::Graph { props }
| node @ WidgetNode::GtkUI { props }
| node @ WidgetNode::Transform { props } => {
let new_props = with_dyn_id(props.clone(), parent_path);
match node {
@@ -129,6 +130,7 @@ impl WidgetNode {
WidgetNode::CircularProgress { props: new_props }
}
WidgetNode::Graph { .. } => WidgetNode::Graph { props: new_props },
WidgetNode::GtkUI { .. } => WidgetNode::GtkUI { props: new_props },
WidgetNode::Transform { .. } => WidgetNode::Transform { props: new_props },
_ => unreachable!(),
}