Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

upd: add initial params when create multiple windows. #149

Merged
merged 1 commit into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions examples/multi_window/my_widget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,15 @@ impl WidgetImpl for MyWidget {
.build(),
)
.modal(true)
.param("key-1", 10.)
.param("key-2", "value")
.on_activate(|window| {
println!(
"{} => Child window created.",
std::thread::current().name().unwrap(),
);
assert_eq!(window.get_param::<f64>("key-1").unwrap(), 10.);
assert_eq!(window.get_param::<String>("key-2").unwrap(), "value");
window.child(InputDialog::new());
}),
)
Expand Down
14 changes: 13 additions & 1 deletion tmui/src/application_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ use tlib::{
namespace::{KeyboardModifier, MouseButton},
nonnull_mut, nonnull_ref,
object::{ObjectImpl, ObjectSubclass},
values::FromValue,
winit::window::WindowId,
};

Expand All @@ -53,6 +54,7 @@ pub struct ApplicationWindow {
shared_widget_size_changed: bool,
high_load_request: bool,
outer_position: Point,
params: Option<HashMap<String, Value>>,

board: Option<NonNull<Board>>,
output_sender: Option<OutputSender>,
Expand Down Expand Up @@ -345,6 +347,11 @@ impl ApplicationWindow {
self.outer_position
}

#[inline]
pub fn get_param<T: FromValue + StaticType>(&self, key: &str) -> Option<T> {
self.params.as_ref()?.get(key).map(|p| p.get::<T>())
}

/// Should set the parent of widget before use this function.
pub fn initialize_dynamic_component(widget: &mut dyn WidgetImpl) {
INTIALIZE_PHASE.with(|p| {
Expand Down Expand Up @@ -629,7 +636,7 @@ impl ApplicationWindow {
continue;
}
if overlaid.rect().contains(point) {
return
return;
}
}

Expand Down Expand Up @@ -731,6 +738,11 @@ impl ApplicationWindow {
pub(crate) fn root_ancestors(&self) -> &[ObjectId] {
&self.root_ancestors
}

#[inline]
pub(crate) fn set_params(&mut self, params: Option<HashMap<String, Value>>) {
self.params = params
}
}

/// Get window id in current ui thread.
Expand Down
27 changes: 21 additions & 6 deletions tmui/src/input/number.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@ impl WidgetImpl for Number {

self.handle_key_pressed(event);

self.handle_number_key_pressed(event);

self.update();
}

Expand All @@ -171,7 +173,7 @@ impl WidgetImpl for Number {
_ => {}
}

self.handle_spinner_press(event);
self.handle_spinner_press(Some(event), None);
}

#[inline]
Expand Down Expand Up @@ -505,11 +507,16 @@ impl Number {
}
}

fn handle_spinner_press(&mut self, evt: &MouseEvent) {
let pos = self.map_to_global_f(&evt.position().into());
let (spinner1, spinner2) = self.spinner_rect();
let spinner1_effect = spinner1.contains(&pos);
let spinner2_effect = spinner2.contains(&pos);
fn handle_spinner_press(&mut self, evt: Option<&MouseEvent>, spurious_press: Option<bool>) {
let (spinner1_effect, spinner2_effect) = if let Some(evt) = evt {
let pos = self.map_to_global_f(&evt.position().into());
let (spinner1, spinner2) = self.spinner_rect();
(spinner1.contains(&pos), spinner2.contains(&pos))
} else if let Some(spurious_press) = spurious_press {
(spurious_press, !spurious_press)
} else {
(false, false)
};

if !spinner1_effect && !spinner2_effect {
return;
Expand Down Expand Up @@ -574,6 +581,14 @@ impl Number {
}
}
}

fn handle_number_key_pressed(&mut self, event: &KeyEvent) {
match event.key_code() {
KeyCode::KeyUp => self.handle_spinner_press(None, Some(true)),
KeyCode::KeyDown => self.handle_spinner_press(None, Some(false)),
_ => {}
}
}
}

fn calc_text_window_with_spinner(props: &TextProps, rect: FRect) -> FRect {
Expand Down
7 changes: 5 additions & 2 deletions tmui/src/platform/logic_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ use crate::{
runtime::window_context::LogicWindowContext,
};
use glutin::config::Config;
use std::sync::Arc;
use std::{collections::HashMap, sync::Arc};
use tipc::{
ipc_master::IpcMaster, ipc_slave::IpcSlave, mem::mem_rw_lock::MemRwLock, parking_lot::RwLock,
IpcNode, IpcType,
};
use tlib::{figure::Point, winit::window::WindowId};
use tlib::{figure::Point, winit::window::WindowId, Value};

pub(crate) struct LogicWindow<T: 'static + Copy + Sync + Send, M: 'static + Copy + Sync + Send> {
window_id: Option<WindowId>,
Expand All @@ -41,6 +41,7 @@ pub(crate) struct LogicWindow<T: 'static + Copy + Sync + Send, M: 'static + Copy
pub on_request_receive: Option<FnRequestReceive<M>>,

pub initial_position: Point,
pub params: Option<HashMap<String, Value>>,
}

unsafe impl<T: 'static + Copy + Sync + Send, M: 'static + Copy + Sync + Send> Send
Expand Down Expand Up @@ -77,6 +78,7 @@ impl<T: 'static + Copy + Sync + Send, M: 'static + Copy + Sync + Send> LogicWind
on_user_event_receive: None,
on_request_receive: None,
initial_position,
params: None,
}
}

Expand Down Expand Up @@ -106,6 +108,7 @@ impl<T: 'static + Copy + Sync + Send, M: 'static + Copy + Sync + Send> LogicWind
on_user_event_receive: None,
on_request_receive: None,
initial_position: Point::default(),
params: None,
}
}

Expand Down
1 change: 1 addition & 0 deletions tmui/src/runtime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ where
window.register_output(output_sender);
window.set_ipc_bridge(logic_window.create_ipc_bridge());
window.set_outer_position(logic_window.initial_position);
window.set_params(logic_window.params.take());

if let Some(window_id) = logic_window.window_id() {
window.set_winit_id(window_id)
Expand Down
1 change: 1 addition & 0 deletions tmui/src/runtime/windows_process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,7 @@ impl<'a, T: 'static + Copy + Send + Sync, M: 'static + Copy + Send + Sync>

logic_window.set_parent_window(win.get_parent());
logic_window.on_activate = win.take_on_activate();
logic_window.params = win.take_params();

let phys_window = physical_window.into_phys_window();
let win_id = phys_window.window_id();
Expand Down
13 changes: 12 additions & 1 deletion tmui/src/window/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
pub mod win_builder;
pub mod win_config;

use tlib::winit::window::WindowId;
use tlib::{winit::window::WindowId, Value};

use self::{win_builder::WindowBuilder, win_config::WindowConfig};
use crate::application::FnActivate;
use std::{
collections::HashMap,
fmt::Debug,
sync::atomic::{AtomicUsize, Ordering},
};
Expand All @@ -18,6 +19,7 @@ pub struct Window {
win_cfg: Option<WindowConfig>,
on_activate: Option<FnActivate>,
parent: Option<WindowId>,
params: Option<HashMap<String, Value>>,
}

unsafe impl Send for Window {}
Expand All @@ -43,6 +45,7 @@ impl Window {
win_cfg: None,
on_activate: None,
parent: None,
params: None,
}
}
}
Expand Down Expand Up @@ -73,6 +76,14 @@ impl Window {
self.parent.unwrap()
}

#[inline]
pub(crate) fn take_params(&mut self) -> Option<HashMap<String, Value>> {
self.params
.take()
}
}

impl Window {
#[inline]
pub fn index(&self) -> usize {
self.index
Expand Down
10 changes: 10 additions & 0 deletions tmui/src/window/win_builder.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
use super::{win_config::WindowConfig, Window};
use crate::{application::FnActivate, application_window::ApplicationWindow};
use std::collections::HashMap;
use tlib::{values::ToValue, Value};

#[derive(Default)]
pub struct WindowBuilder {
win_cfg: Option<WindowConfig>,
modal: bool,
on_activate: Option<FnActivate>,
params: HashMap<String, Value>,
}

impl WindowBuilder {
Expand Down Expand Up @@ -39,6 +42,12 @@ impl WindowBuilder {
self
}

#[inline]
pub fn param(mut self, key: impl ToString, val: impl ToValue) -> Self {
self.params.insert(key.to_string(), val.to_value());
self
}

#[inline]
pub fn build(self) -> Window {
let mut window = Window::new();
Expand All @@ -49,6 +58,7 @@ impl WindowBuilder {
);
window.on_activate = self.on_activate;
window.modal = self.modal;
window.params = Some(self.params);

window
}
Expand Down
Loading