This commit is contained in:
Ferdinand Schober
2025-03-14 15:26:48 +01:00
parent f6c526b596
commit 7e9ad6f1a2
3 changed files with 65 additions and 58 deletions

View File

@@ -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);
}
}

View File

@@ -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<gtk::Spinner>,
pub bindings: RefCell<Vec<Binding>>,
pub hostname_change_handler: RefCell<Option<SignalHandlerId>>,
pub port_change_handler: RefCell<Option<SignalHandlerId>>,
pub position_change_handler: RefCell<Option<SignalHandlerId>>,
pub set_state_handler: RefCell<Option<SignalHandlerId>>,
hostname_change_handler: RefCell<Option<SignalHandlerId>>,
port_change_handler: RefCell<Option<SignalHandlerId>>,
position_change_handler: RefCell<Option<SignalHandlerId>>,
set_state_handler: RefCell<Option<SignalHandlerId>>,
pub client_object: RefCell<Option<ClientObject>>,
}
#[glib::object_subclass]
@@ -87,7 +91,6 @@ impl ObjectImpl for ClientRow {
}
));
self.position_change_handler.replace(Some(handler));
// <signal name="state_set" handler="handle_activate_switch" swapped="true"/>
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);
}
}

View File

@@ -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