Skip to content

Commit

Permalink
getting closer
Browse files Browse the repository at this point in the history
  • Loading branch information
feschber committed Jan 15, 2024
1 parent 4178199 commit 77a420a
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 28 deletions.
15 changes: 1 addition & 14 deletions src/frontend/gtk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use adw::Application;
use gtk::{
gdk::Display,
gio::{SimpleAction, SimpleActionGroup},
glib::clone,
glib::{clone, closure_local},
prelude::*,
subclass::prelude::ObjectSubclassIsExt,
CssProvider, IconTheme,
Expand Down Expand Up @@ -180,19 +180,6 @@ fn build_ui(app: &Application) {
let action_client_delete =
SimpleAction::new("request-client-delete", Some(&u32::static_variant_type()));

// update client state
action_request_client_update.connect_activate(clone!(@weak window => move |_action, param| {
log::debug!("request-client-update");
let index = param.unwrap()
.get::<u32>()
.unwrap();
let Some(client) = window.clients().item(index) else {
return;
};
let client = client.downcast_ref::<ClientObject>().unwrap();
window.request_client_update(client);
}));

action_client_delete.connect_activate(clone!(@weak window => move |_action, param| {
log::debug!("delete-client");
let idx = param.unwrap()
Expand Down
23 changes: 23 additions & 0 deletions src/frontend/gtk/client_row.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,28 @@ impl ClientRow {

let active_binding = client_object
.bind_property("active", &self.imp().enable_switch.get(), "state")
.transform_to(|_, a: bool| {
log::warn!("setting switch state to {a}");
Some(a)
})
.transform_to(|_, a: bool| {
log::warn!("state changed -> setting client_object active to {a}");
Some(a)
})
.bidirectional()
.sync_create()
.build();

let switch_position_binding = client_object
.bind_property("active", &self.imp().enable_switch.get(), "active")
.transform_to(|_, a: bool| {
log::warn!("setting switch active to {a}");
Some(a)
})
.transform_to(|_, a: bool| {
log::warn!("active changed -> setting client_object active to {a}");
Some(a)
})
.bidirectional()
.sync_create()
.build();
Expand Down Expand Up @@ -104,6 +126,7 @@ impl ClientRow {
.build();

bindings.push(active_binding);
bindings.push(switch_position_binding);
bindings.push(hostname_binding);
bindings.push(title_binding);
bindings.push(port_binding);
Expand Down
25 changes: 19 additions & 6 deletions src/frontend/gtk/client_row/imp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use adw::subclass::prelude::*;
use adw::{prelude::*, ActionRow, ComboRow};
use glib::{subclass::InitializingObject, Binding};
use gtk::glib::clone;
use gtk::glib::once_cell::sync::Lazy;
use gtk::glib::subclass::Signal;
use gtk::{glib, Button, CompositeTemplate, Switch};

#[derive(CompositeTemplate, Default)]
Expand All @@ -28,6 +30,8 @@ pub struct ClientRow {
impl ObjectSubclass for ClientRow {
// `NAME` needs to match `class` attribute of template
const NAME: &'static str = "ClientRow";
const ABSTRACT: bool = false;

type Type = super::ClientRow;
type ParentType = adw::ExpanderRow;

Expand All @@ -49,19 +53,28 @@ impl ObjectImpl for ClientRow {
row.handle_client_delete(button);
}));
}

fn signals() -> &'static [glib::subclass::Signal] {
static SIGNALS: Lazy<Vec<Signal>> = Lazy::new(|| {
vec![Signal::builder("request-update")
.param_types([u32::static_type(), bool::static_type()])
.build()]
});
SIGNALS.as_ref()
}
}

#[gtk::template_callbacks]
impl ClientRow {
#[template_callback]
fn handle_client_set_state(&self, state: bool, switch: &Switch) -> bool {
let idx = self.obj().index() as u32;
if let Err(e) = switch.activate_action("win.request-client-update", Some(&idx.to_variant()))
{
log::error!("{e}");
}
switch.set_state(state);
log::warn!("state: {state}, active: {}", switch.is_active());
switch.set_active(state);
switch.set_state(state);

log::warn!("REQUESTING CLIENT UPDATE");
let idx = self.obj().index() as u32;
self.obj().emit_by_name::<()>("request-update", &[&idx, &state]);

true // dont run default handler
}
Expand Down
35 changes: 28 additions & 7 deletions src/frontend/gtk/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::io::Write;
use adw::prelude::*;
use adw::subclass::prelude::*;
use glib::{clone, Object};
use gtk::{gio, glib, NoSelection};
use gtk::{gio, glib::{self, closure_local}, NoSelection};

use crate::{
client::{Client, ClientHandle, Position},
Expand Down Expand Up @@ -45,6 +45,13 @@ impl Window {
clone!(@weak self as window => @default-panic, move |obj| {
let client_object = obj.downcast_ref().expect("Expected object of type `ClientObject`.");
let row = window.create_client_row(client_object);
row.connect_closure("request-update", false, closure_local!(@strong window => move |_row: ClientRow, index: u32, active: bool| {
let Some(client) = window.clients().item(index) else {
return;
};
let client = client.downcast_ref::<ClientObject>().unwrap();
window.request_client_update(client, active);
}));
row.upcast()
})
);
Expand Down Expand Up @@ -113,10 +120,23 @@ impl Window {
};
let client_object = self.clients().item(idx as u32).unwrap();
let client_object: &ClientObject = client_object.downcast_ref().unwrap();
client_object.set_hostname(client.hostname.unwrap_or("".into()));
client_object.set_port(client.port as u32);
client_object.set_position(client.pos.to_string());
client_object.set_active(active);
let data = client_object.get_data();

/* only change if it actually has changed, otherwise
* the update signal is triggered */
if data.hostname != client.hostname {
client_object.set_hostname(client.hostname.unwrap_or("".into()));
}
if data.port != client.port as u32 {
client_object.set_port(client.port as u32);
}
if data.position != client.pos.to_string() {
client_object.set_position(client.pos.to_string());
}
if data.active != active {
client_object.set_active(active);
log::warn!("set active to {active}");
}
}

pub fn request_client_create(&self) {
Expand All @@ -134,7 +154,7 @@ impl Window {
}
}

pub fn request_client_update(&self, client: &ClientObject) {
pub fn request_client_update(&self, client: &ClientObject, active: bool) {
let data = client.get_data();
let position = match Position::try_from(data.position.as_str()) {
Ok(pos) => pos,
Expand All @@ -148,7 +168,8 @@ impl Window {
let event = FrontendEvent::UpdateClient(client.handle(), hostname, port, position);
self.request(event);

let event = FrontendEvent::ActivateClient(client.handle(), !client.active());
let event = FrontendEvent::ActivateClient(client.handle(), active);
log::warn!("requesting update: {event:?}");
self.request(event);
}

Expand Down
15 changes: 14 additions & 1 deletion src/frontend/gtk/window/imp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ use std::os::unix::net::UnixStream;

use adw::subclass::prelude::*;
use adw::{
prelude::{EditableExt, WidgetExt},
prelude::*,
ActionRow, ToastOverlay,
};
use glib::subclass::InitializingObject;
use gtk::glib::once_cell::sync::Lazy;
use gtk::glib::subclass::Signal;
use gtk::{gio, glib, Button, CompositeTemplate, Entry, ListBox};

use crate::config::DEFAULT_PORT;
Expand Down Expand Up @@ -42,6 +44,8 @@ pub struct Window {
impl ObjectSubclass for Window {
// `NAME` needs to match `class` attribute of template
const NAME: &'static str = "LanMouseWindow";
const ABSTRACT: bool = false;

type Type = super::Window;
type ParentType = adw::ApplicationWindow;

Expand Down Expand Up @@ -102,6 +106,15 @@ impl ObjectImpl for Window {
obj.setup_icon();
obj.setup_clients();
}

fn signals() -> &'static [glib::subclass::Signal] {
static SIGNALS: Lazy<Vec<Signal>> = Lazy::new(|| {
vec![Signal::builder("request-update")
.param_types([u32::static_type(), bool::static_type()])
.build()]
});
SIGNALS.as_ref()
}
}

impl WidgetImpl for Window {}
Expand Down

0 comments on commit 77a420a

Please sign in to comment.