From 4041190f573c90084c0ae045930dfc8d718ca061 Mon Sep 17 00:00:00 2001 From: sleptworld Date: Thu, 18 Jan 2024 08:56:03 +0800 Subject: [PATCH] test css --- .../render_panel/monitor/monitor.rs | 12 +-- .../render_panel/monitor/sidebar/sidebar.rs | 9 ++- src/dynamic_col/imp.rs | 58 +++++++++++--- src/dynamic_col/mod.rs | 76 ++++++++++++++----- src/main.rs | 6 +- 5 files changed, 117 insertions(+), 44 deletions(-) diff --git a/src/components/render_panel/monitor/monitor.rs b/src/components/render_panel/monitor/monitor.rs index 673f753..378ade1 100644 --- a/src/components/render_panel/monitor/monitor.rs +++ b/src/components/render_panel/monitor/monitor.rs @@ -43,13 +43,12 @@ impl AsyncComponent for MonitorModel { #[wrap(Some)] #[name="test"] set_child = &DynamicCol{ - set_ratio: 0.5, + set_end_width: 300, set_hexpand: true, set_vexpand: true, #[wrap(Some)] #[name="paned"] - set_child=>k::Paned{ - set_position: top_level_window_unwrap.default_width() - model.sidebar_width, + set_child_paned=>k::Paned{ #[wrap(Some)] #[name="render"] set_start_child=>k::Frame{ @@ -88,14 +87,7 @@ impl AsyncComponent for MonitorModel { sidebar, }; - let top_level_window = root.toplevel_window(); - let top_level_window_unwrap = top_level_window.unwrap(); - let widgets = view_output! {}; - top_level_window_unwrap.connect_width_request_notify(|w| { - println!("window width: {}", w.width_request()); - }); - println!("window width: {}", widgets.test.get_start_width()); AsyncComponentParts { model, widgets } } diff --git a/src/components/render_panel/monitor/sidebar/sidebar.rs b/src/components/render_panel/monitor/sidebar/sidebar.rs index 47a88e4..0b3f0c2 100644 --- a/src/components/render_panel/monitor/sidebar/sidebar.rs +++ b/src/components/render_panel/monitor/sidebar/sidebar.rs @@ -52,8 +52,11 @@ impl SimpleComponent for SideBarModel { set_start_child = >k::Box{ set_orientation: gtk::Orientation::Vertical, set_spacing: 5, - #[local] - top_panel -> gtk::Notebook{}, + gtk::Frame{ + add_css_class: "rb", + #[local] + top_panel -> gtk::Notebook{} + }, gtk::Button { set_label: "Add Layers", connect_clicked[sender] => move |_| { @@ -124,7 +127,7 @@ impl SimpleComponent for SideBarModel { }; let my_view = &model.list_view_wrapper.view; - let top_panel = gtk::Notebook::builder().vexpand(true).build(); + let top_panel = gtk::Notebook::builder().vexpand(true).hexpand(true).build(); top_panel.append_page(&Chart::new(), Some(>k::Label::new(Some("Chart")))); let bottom_panel = gtk::Notebook::builder().vexpand(true).build(); diff --git a/src/dynamic_col/imp.rs b/src/dynamic_col/imp.rs index b450b0c..315a2e4 100644 --- a/src/dynamic_col/imp.rs +++ b/src/dynamic_col/imp.rs @@ -1,23 +1,26 @@ use adw::subclass::bin::BinImpl; -use gtk::glib; +use gtk::glib::prelude::*; +use gtk::prelude::*; use gtk::subclass::prelude::*; use std::cell::{Cell, RefCell}; use std::num::NonZeroU32; pub struct DynamicCol { + pub(super) child: RefCell>, pub(super) ratio: RefCell>, width: Cell, - pub(super) start_width: Cell, - pub(super) end_width: Cell, + pub(super) start_width: Cell>, + pub(super) end_width: Cell>, } impl Default for DynamicCol { fn default() -> Self { Self { + child: RefCell::new(None), ratio: RefCell::new(None), width: Cell::new(0), - start_width: Cell::new(0), - end_width: Cell::new(0), + start_width: Cell::new(None), + end_width: Cell::new(None), } } } @@ -26,13 +29,48 @@ impl Default for DynamicCol { impl ObjectSubclass for DynamicCol { const NAME: &'static str = "DynamicCol"; type Type = super::DynamicCol; - type ParentType = gtk::Box; + type ParentType = gtk::Widget; } -impl ObjectImpl for DynamicCol {} +impl ObjectImpl for DynamicCol { + fn dispose(&self) { + if let Some(child) = self.child.borrow_mut().take() { + child.unparent(); + } + } +} -impl WidgetImpl for DynamicCol {} +impl WidgetImpl for DynamicCol { + fn measure(&self, orientation: gtk::Orientation, for_size: i32) -> (i32, i32, i32, i32) { + let widget = self.obj(); + let child = self.child.borrow(); + let child = match child.as_ref() { + Some(child) => child, + None => return (0, 0, -1, -1), + }; -impl BoxImpl for DynamicCol {} + child.measure(orientation, for_size) + } -impl WidgetImplExt for DynamicCol{} + fn size_allocate(&self, width: i32, height: i32, baseline: i32) { + let widget = self.obj(); + let child = self.child.borrow(); + + let child = match child.as_ref() { + Some(child) => child, + None => return, + }; + child.size_allocate(>k::Allocation::new(0, 0, width, height), baseline); + + let ratio = self.ratio.borrow(); + + let position = if let Some(ratio) = *ratio { + (width as f64 * ratio) as i32 + } else { + self.start_width.get().or(Some(width - self.end_width.get().unwrap())).unwrap() + }; + + child.set_position(position); + + } +} diff --git a/src/dynamic_col/mod.rs b/src/dynamic_col/mod.rs index b6ba7ba..0b2a377 100644 --- a/src/dynamic_col/mod.rs +++ b/src/dynamic_col/mod.rs @@ -1,14 +1,12 @@ -mod imp; mod custom_layout; -use glib::clone; -pub use glib::subclass::prelude::*; -use gtk::traits::WidgetExt; +mod imp; +use gtk::{glib, prelude::*, subclass::prelude::*}; use std::cell::{Ref, RefCell, RefMut}; use std::sync::{Arc, Mutex}; glib::wrapper! { pub struct DynamicCol(ObjectSubclass) - @extends gtk::Box, gtk::Widget; + @extends gtk::Widget; } impl Default for DynamicCol { @@ -24,29 +22,69 @@ impl DynamicCol { } pub fn set_ratio(&self, ratio: f64) { - let self_ = imp::DynamicCol::from_instance(self); + let self_ = self.imp(); self_.ratio.replace(Some(ratio)); self.queue_resize(); } + pub fn set_start_width(&self, ratio: i32) { + let self_ = self.imp(); + self_.start_width.replace(Some(ratio)); + self.queue_resize(); + } + + pub fn set_end_width(&self, ratio: i32) { + let self_ = self.imp(); + self_.end_width.replace(Some(ratio)); + self.queue_resize(); + } + pub fn get_ratio(&self) -> Option { - let self_ = imp::DynamicCol::from_instance(self); + let self_ = self.imp(); *self_.ratio.borrow() } - pub fn get_start_width(&self) -> i32 { - let self_ = imp::DynamicCol::from_instance(self); - self_.start_width.get() - } - - pub fn get_end_width(&self) -> i32 { - let self_ = imp::DynamicCol::from_instance(self); - self_.end_width.get() - } - - fn test(&self) { + pub fn set_child_paned(&self, widget: Option<>k::Paned>) { let imp = self.imp(); + let widget = widget.map(|w| w.upcast_ref()); - let manager = imp.layout_manager(); + if widget == imp.child.borrow().as_ref() { + return; + } + + if let Some(child) = imp.child.borrow_mut().take() { + child.unparent(); + } + + if let Some(w) = widget { + imp.child.replace(Some(w.clone())); + w.set_parent(self); + } + + self.queue_resize(); + self.notify("child") } + + // pub fn set_child(&self, widget: Option<&impl IsA>) { + + // let imp = self.imp(); + // let widget = widget.map(|w| w.upcast_ref()); + + // if widget == imp.child.borrow().as_ref() { + // return; + // } + + // if let Some(child) = imp.child.borrow_mut().take() { + // child.unparent(); + // } + + // if let Some(w) = widget { + // imp.child.replace(Some(w.clone())); + // w.set_parent(self); + // } + + // self.queue_resize(); + // self.notify("child") + // } + } diff --git a/src/main.rs b/src/main.rs index 952038d..4f9af71 100644 --- a/src/main.rs +++ b/src/main.rs @@ -28,8 +28,10 @@ fn main() { { #[cfg(target_os = "macos")] let library = - unsafe { libloading::os::unix::Library::new("/opt/homebrew/lib/libepoxy.0.dylib") } - .unwrap(); + unsafe { libloading::os::unix::Library::new("/opt/homebrew/lib/libepoxy.0.dylib") }.or(unsafe{ + libloading::os::unix::Library::new("libepoxy.0.dylib") + }).unwrap(); + #[cfg(all(unix, not(target_os = "macos")))] let library = unsafe { libloading::os::unix::Library::new("libepoxy.so.0") }.unwrap(); #[cfg(windows)]