From 7e9ad6f1a25cb1d16026fb389c33f759435964a5 Mon Sep 17 00:00:00 2001 From: Ferdinand Schober Date: Fri, 14 Mar 2025 15:26:48 +0100 Subject: [PATCH] improve --- lan-mouse-gtk/src/client_row.rs | 28 ++++++++++-- lan-mouse-gtk/src/client_row/imp.rs | 66 ++++++++++++++++------------- lan-mouse-gtk/src/window.rs | 29 ++----------- 3 files changed, 65 insertions(+), 58 deletions(-) diff --git a/lan-mouse-gtk/src/client_row.rs b/lan-mouse-gtk/src/client_row.rs index 6fa608a..5b99130 100644 --- a/lan-mouse-gtk/src/client_row.rs +++ b/lan-mouse-gtk/src/client_row.rs @@ -4,7 +4,7 @@ use adw::prelude::*; use adw::subclass::prelude::*; use gtk::glib::{self, Object}; -use lan_mouse_ipc::DEFAULT_PORT; +use lan_mouse_ipc::{Position, DEFAULT_PORT}; use super::ClientObject; @@ -15,8 +15,14 @@ glib::wrapper! { } impl ClientRow { - pub fn new(_client_object: &ClientObject) -> Self { - Object::builder().build() + pub fn new(client_object: &ClientObject) -> Self { + let client_row: Self = Object::builder().build(); + client_row + .imp() + .client_object + .borrow_mut() + .replace(client_object.clone()); + client_row } pub fn bind(&self, client_object: &ClientObject) { @@ -130,4 +136,20 @@ impl ClientRow { binding.unbind(); } } + + pub fn set_active(&self, active: bool) { + self.imp().set_active(active); + } + + pub fn set_hostname(&self, hostname: &str) { + self.imp().set_hostname(hostname); + } + + pub fn set_port(&self, port: u16) { + self.imp().set_port(port); + } + + pub fn set_position(&self, pos: Position) { + self.imp().set_pos(pos); + } } diff --git a/lan-mouse-gtk/src/client_row/imp.rs b/lan-mouse-gtk/src/client_row/imp.rs index 629ab95..fb44ed3 100644 --- a/lan-mouse-gtk/src/client_row/imp.rs +++ b/lan-mouse-gtk/src/client_row/imp.rs @@ -6,8 +6,11 @@ use glib::{subclass::InitializingObject, Binding}; use gtk::glib::subclass::Signal; use gtk::glib::{clone, SignalHandlerId}; use gtk::{glib, Button, CompositeTemplate, Entry, Switch}; +use lan_mouse_ipc::Position; use std::sync::OnceLock; +use crate::client_object::ClientObject; + #[derive(CompositeTemplate, Default)] #[template(resource = "/de/feschber/LanMouse/client_row.ui")] pub struct ClientRow { @@ -28,10 +31,11 @@ pub struct ClientRow { #[template_child] pub dns_loading_indicator: TemplateChild, pub bindings: RefCell>, - pub hostname_change_handler: RefCell>, - pub port_change_handler: RefCell>, - pub position_change_handler: RefCell>, - pub set_state_handler: RefCell>, + hostname_change_handler: RefCell>, + port_change_handler: RefCell>, + position_change_handler: RefCell>, + set_state_handler: RefCell>, + pub client_object: RefCell>, } #[glib::object_subclass] @@ -87,7 +91,6 @@ impl ObjectImpl for ClientRow { } )); self.position_change_handler.replace(Some(handler)); - // let handler = self.enable_switch.connect_state_set(clone!( #[weak(rename_to = row)] self, @@ -150,7 +153,6 @@ impl ClientRow { } fn handle_hostname_changed(&self, hostname_entry: &Entry) { - log::error!("hostname changed: {}", hostname_entry.text()); self.obj() .emit_by_name::<()>("request-hostname-change", &[&hostname_entry.text()]); } @@ -160,51 +162,55 @@ impl ClientRow { .emit_by_name("request-position-change", &[&position.selected()]) } - pub fn block_hostname_change(&self) { + pub(super) fn set_hostname(&self, hostname: &str) { + let position = self.hostname.position(); let handler = self.hostname_change_handler.borrow(); let handler = handler.as_ref().expect("signal handler"); self.hostname.block_signal(handler); - } - - pub fn unblock_hostname_change(&self) { - let handler = self.hostname_change_handler.borrow(); - let handler = handler.as_ref().expect("signal handler"); + self.client_object + .borrow_mut() + .as_mut() + .expect("client object") + .set_hostname(hostname); self.hostname.unblock_signal(handler); + self.hostname.set_position(position); } - pub fn block_port_change(&self) { + pub(super) fn set_port(&self, port: u16) { + let position = self.port.position(); let handler = self.port_change_handler.borrow(); let handler = handler.as_ref().expect("signal handler"); self.port.block_signal(handler); - } - - pub fn unblock_port_change(&self) { - let handler = self.port_change_handler.borrow(); - let handler = handler.as_ref().expect("signal handler"); + self.client_object + .borrow_mut() + .as_mut() + .expect("client object") + .set_port(port as u32); self.port.unblock_signal(handler); + self.port.set_position(position); } - pub fn block_position_change(&self) { + pub(super) fn set_pos(&self, pos: Position) { let handler = self.position_change_handler.borrow(); let handler = handler.as_ref().expect("signal handler"); self.position.block_signal(handler); - } - - pub fn unblock_position_change(&self) { - let handler = self.position_change_handler.borrow(); - let handler = handler.as_ref().expect("signal handler"); + self.client_object + .borrow_mut() + .as_mut() + .expect("client object") + .set_position(pos.to_string()); self.position.unblock_signal(handler); } - pub fn block_active_switch(&self) { + pub(super) fn set_active(&self, active: bool) { let handler = self.set_state_handler.borrow(); let handler = handler.as_ref().expect("signal handler"); self.enable_switch.block_signal(handler); - } - - pub fn unblock_active_switch(&self) { - let handler = self.set_state_handler.borrow(); - let handler = handler.as_ref().expect("signal handler"); + self.client_object + .borrow_mut() + .as_mut() + .expect("client object") + .set_active(active); self.enable_switch.unblock_signal(handler); } } diff --git a/lan-mouse-gtk/src/window.rs b/lan-mouse-gtk/src/window.rs index 580ad9c..546ba14 100644 --- a/lan-mouse-gtk/src/window.rs +++ b/lan-mouse-gtk/src/window.rs @@ -298,22 +298,9 @@ impl Window { log::warn!("could not find row for handle {}", handle); return; }; - let Some(client_object) = self.client_object_for_handle(handle) else { - log::warn!("could not find row for handle {}", handle); - return; - }; - - row.imp().block_hostname_change(); - client_object.set_hostname(client.hostname.unwrap_or("".into())); - row.imp().unblock_hostname_change(); - - row.imp().block_port_change(); - client_object.set_port(client.port as u32); - row.imp().unblock_port_change(); - - row.imp().block_position_change(); - client_object.set_position(client.pos.to_string()); - row.imp().unblock_position_change(); + row.set_hostname(&client.hostname.unwrap_or("".into())); + row.set_port(client.port); + row.set_position(client.pos); } pub fn update_client_state(&self, handle: ClientHandle, state: ClientState) { @@ -327,18 +314,10 @@ impl Window { }; /* activation state */ - row.imp().block_active_switch(); - client_object.set_active(state.active); - row.imp().unblock_active_switch(); - log::info!("set active to {}", state.active); + row.set_active(state.active); /* dns state */ client_object.set_resolving(state.resolving); - log::info!( - "resolving {}: {}", - client_object.get_data().handle, - state.resolving - ); self.update_dns_state(handle, !state.ips.is_empty()); let ips = state