Skip to content

Commit 6eef648

Browse files
committed
improve
1 parent ccac5f2 commit 6eef648

File tree

3 files changed

+65
-58
lines changed

3 files changed

+65
-58
lines changed

lan-mouse-gtk/src/client_row.rs

+25-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use adw::prelude::*;
44
use adw::subclass::prelude::*;
55
use gtk::glib::{self, Object};
66

7-
use lan_mouse_ipc::DEFAULT_PORT;
7+
use lan_mouse_ipc::{Position, DEFAULT_PORT};
88

99
use super::ClientObject;
1010

@@ -15,8 +15,14 @@ glib::wrapper! {
1515
}
1616

1717
impl ClientRow {
18-
pub fn new(_client_object: &ClientObject) -> Self {
19-
Object::builder().build()
18+
pub fn new(client_object: &ClientObject) -> Self {
19+
let client_row: Self = Object::builder().build();
20+
client_row
21+
.imp()
22+
.client_object
23+
.borrow_mut()
24+
.replace(client_object.clone());
25+
client_row
2026
}
2127

2228
pub fn bind(&self, client_object: &ClientObject) {
@@ -130,4 +136,20 @@ impl ClientRow {
130136
binding.unbind();
131137
}
132138
}
139+
140+
pub fn set_active(&self, active: bool) {
141+
self.imp().set_active(active);
142+
}
143+
144+
pub fn set_hostname(&self, hostname: &str) {
145+
self.imp().set_hostname(hostname);
146+
}
147+
148+
pub fn set_port(&self, port: u16) {
149+
self.imp().set_port(port);
150+
}
151+
152+
pub fn set_position(&self, pos: Position) {
153+
self.imp().set_pos(pos);
154+
}
133155
}

lan-mouse-gtk/src/client_row/imp.rs

+36-30
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@ use glib::{subclass::InitializingObject, Binding};
66
use gtk::glib::subclass::Signal;
77
use gtk::glib::{clone, SignalHandlerId};
88
use gtk::{glib, Button, CompositeTemplate, Entry, Switch};
9+
use lan_mouse_ipc::Position;
910
use std::sync::OnceLock;
1011

12+
use crate::client_object::ClientObject;
13+
1114
#[derive(CompositeTemplate, Default)]
1215
#[template(resource = "/de/feschber/LanMouse/client_row.ui")]
1316
pub struct ClientRow {
@@ -28,10 +31,11 @@ pub struct ClientRow {
2831
#[template_child]
2932
pub dns_loading_indicator: TemplateChild<gtk::Spinner>,
3033
pub bindings: RefCell<Vec<Binding>>,
31-
pub hostname_change_handler: RefCell<Option<SignalHandlerId>>,
32-
pub port_change_handler: RefCell<Option<SignalHandlerId>>,
33-
pub position_change_handler: RefCell<Option<SignalHandlerId>>,
34-
pub set_state_handler: RefCell<Option<SignalHandlerId>>,
34+
hostname_change_handler: RefCell<Option<SignalHandlerId>>,
35+
port_change_handler: RefCell<Option<SignalHandlerId>>,
36+
position_change_handler: RefCell<Option<SignalHandlerId>>,
37+
set_state_handler: RefCell<Option<SignalHandlerId>>,
38+
pub client_object: RefCell<Option<ClientObject>>,
3539
}
3640

3741
#[glib::object_subclass]
@@ -87,7 +91,6 @@ impl ObjectImpl for ClientRow {
8791
}
8892
));
8993
self.position_change_handler.replace(Some(handler));
90-
// <signal name="state_set" handler="handle_activate_switch" swapped="true"/>
9194
let handler = self.enable_switch.connect_state_set(clone!(
9295
#[weak(rename_to = row)]
9396
self,
@@ -150,7 +153,6 @@ impl ClientRow {
150153
}
151154

152155
fn handle_hostname_changed(&self, hostname_entry: &Entry) {
153-
log::error!("hostname changed: {}", hostname_entry.text());
154156
self.obj()
155157
.emit_by_name::<()>("request-hostname-change", &[&hostname_entry.text()]);
156158
}
@@ -160,51 +162,55 @@ impl ClientRow {
160162
.emit_by_name("request-position-change", &[&position.selected()])
161163
}
162164

163-
pub fn block_hostname_change(&self) {
165+
pub(super) fn set_hostname(&self, hostname: &str) {
166+
let position = self.hostname.position();
164167
let handler = self.hostname_change_handler.borrow();
165168
let handler = handler.as_ref().expect("signal handler");
166169
self.hostname.block_signal(handler);
167-
}
168-
169-
pub fn unblock_hostname_change(&self) {
170-
let handler = self.hostname_change_handler.borrow();
171-
let handler = handler.as_ref().expect("signal handler");
170+
self.client_object
171+
.borrow_mut()
172+
.as_mut()
173+
.expect("client object")
174+
.set_hostname(hostname);
172175
self.hostname.unblock_signal(handler);
176+
self.hostname.set_position(position);
173177
}
174178

175-
pub fn block_port_change(&self) {
179+
pub(super) fn set_port(&self, port: u16) {
180+
let position = self.port.position();
176181
let handler = self.port_change_handler.borrow();
177182
let handler = handler.as_ref().expect("signal handler");
178183
self.port.block_signal(handler);
179-
}
180-
181-
pub fn unblock_port_change(&self) {
182-
let handler = self.port_change_handler.borrow();
183-
let handler = handler.as_ref().expect("signal handler");
184+
self.client_object
185+
.borrow_mut()
186+
.as_mut()
187+
.expect("client object")
188+
.set_port(port as u32);
184189
self.port.unblock_signal(handler);
190+
self.port.set_position(position);
185191
}
186192

187-
pub fn block_position_change(&self) {
193+
pub(super) fn set_pos(&self, pos: Position) {
188194
let handler = self.position_change_handler.borrow();
189195
let handler = handler.as_ref().expect("signal handler");
190196
self.position.block_signal(handler);
191-
}
192-
193-
pub fn unblock_position_change(&self) {
194-
let handler = self.position_change_handler.borrow();
195-
let handler = handler.as_ref().expect("signal handler");
197+
self.client_object
198+
.borrow_mut()
199+
.as_mut()
200+
.expect("client object")
201+
.set_position(pos.to_string());
196202
self.position.unblock_signal(handler);
197203
}
198204

199-
pub fn block_active_switch(&self) {
205+
pub(super) fn set_active(&self, active: bool) {
200206
let handler = self.set_state_handler.borrow();
201207
let handler = handler.as_ref().expect("signal handler");
202208
self.enable_switch.block_signal(handler);
203-
}
204-
205-
pub fn unblock_active_switch(&self) {
206-
let handler = self.set_state_handler.borrow();
207-
let handler = handler.as_ref().expect("signal handler");
209+
self.client_object
210+
.borrow_mut()
211+
.as_mut()
212+
.expect("client object")
213+
.set_active(active);
208214
self.enable_switch.unblock_signal(handler);
209215
}
210216
}

lan-mouse-gtk/src/window.rs

+4-25
Original file line numberDiff line numberDiff line change
@@ -298,22 +298,9 @@ impl Window {
298298
log::warn!("could not find row for handle {}", handle);
299299
return;
300300
};
301-
let Some(client_object) = self.client_object_for_handle(handle) else {
302-
log::warn!("could not find row for handle {}", handle);
303-
return;
304-
};
305-
306-
row.imp().block_hostname_change();
307-
client_object.set_hostname(client.hostname.unwrap_or("".into()));
308-
row.imp().unblock_hostname_change();
309-
310-
row.imp().block_port_change();
311-
client_object.set_port(client.port as u32);
312-
row.imp().unblock_port_change();
313-
314-
row.imp().block_position_change();
315-
client_object.set_position(client.pos.to_string());
316-
row.imp().unblock_position_change();
301+
row.set_hostname(&client.hostname.unwrap_or("".into()));
302+
row.set_port(client.port);
303+
row.set_position(client.pos);
317304
}
318305

319306
pub fn update_client_state(&self, handle: ClientHandle, state: ClientState) {
@@ -327,18 +314,10 @@ impl Window {
327314
};
328315

329316
/* activation state */
330-
row.imp().block_active_switch();
331-
client_object.set_active(state.active);
332-
row.imp().unblock_active_switch();
333-
log::info!("set active to {}", state.active);
317+
row.set_active(state.active);
334318

335319
/* dns state */
336320
client_object.set_resolving(state.resolving);
337-
log::info!(
338-
"resolving {}: {}",
339-
client_object.get_data().handle,
340-
state.resolving
341-
);
342321

343322
self.update_dns_state(handle, !state.ips.is_empty());
344323
let ips = state

0 commit comments

Comments
 (0)