mirror of
https://github.com/feschber/lan-mouse.git
synced 2026-04-07 04:51:27 +03:00
port changing functionality (#34)
* port changing functionality * add portchange to cli frontend
This commit is contained in:
committed by
GitHub
parent
60a73b3cb0
commit
e88241e816
@@ -1,22 +1,32 @@
|
||||
use std::{cell::{Cell, RefCell}, os::unix::net::UnixStream};
|
||||
|
||||
use glib::subclass::InitializingObject;
|
||||
use adw::{prelude::*, ActionRow};
|
||||
use adw::{ActionRow, ToastOverlay, prelude::{WidgetExt, EditableExt}};
|
||||
use adw::subclass::prelude::*;
|
||||
use gtk::{glib, Button, CompositeTemplate, ListBox, gio};
|
||||
use gtk::{glib, Button, CompositeTemplate, ListBox, gio, Entry};
|
||||
|
||||
use crate::config::DEFAULT_PORT;
|
||||
|
||||
#[derive(CompositeTemplate, Default)]
|
||||
#[template(resource = "/de/feschber/LanMouse/window.ui")]
|
||||
pub struct Window {
|
||||
pub number: Cell<i32>,
|
||||
#[template_child]
|
||||
pub port_edit_apply: TemplateChild<Button>,
|
||||
#[template_child]
|
||||
pub port_edit_cancel: TemplateChild<Button>,
|
||||
#[template_child]
|
||||
pub add_client_button: TemplateChild<Button>,
|
||||
#[template_child]
|
||||
pub client_list: TemplateChild<ListBox>,
|
||||
#[template_child]
|
||||
pub client_placeholder: TemplateChild<ActionRow>,
|
||||
#[template_child]
|
||||
pub port_entry: TemplateChild<Entry>,
|
||||
#[template_child]
|
||||
pub toast_overlay: TemplateChild<ToastOverlay>,
|
||||
pub clients: RefCell<Option<gio::ListStore>>,
|
||||
pub stream: RefCell<Option<UnixStream>>,
|
||||
pub port: Cell<u16>,
|
||||
}
|
||||
|
||||
#[glib::object_subclass]
|
||||
@@ -24,7 +34,7 @@ impl ObjectSubclass for Window {
|
||||
// `NAME` needs to match `class` attribute of template
|
||||
const NAME: &'static str = "LanMouseWindow";
|
||||
type Type = super::Window;
|
||||
type ParentType = gtk::ApplicationWindow;
|
||||
type ParentType = adw::ApplicationWindow;
|
||||
|
||||
fn class_init(klass: &mut Self::Class) {
|
||||
klass.bind_template();
|
||||
@@ -39,10 +49,38 @@ impl ObjectSubclass for Window {
|
||||
#[gtk::template_callbacks]
|
||||
impl Window {
|
||||
#[template_callback]
|
||||
fn handle_button_clicked(&self, button: &Button) {
|
||||
let number_increased = self.number.get() + 1;
|
||||
self.number.set(number_increased);
|
||||
button.set_label(&number_increased.to_string())
|
||||
fn handle_add_client_pressed(&self, _button: &Button) {
|
||||
self.obj().request_client_create();
|
||||
}
|
||||
|
||||
#[template_callback]
|
||||
fn handle_port_changed(&self, _entry: &Entry) {
|
||||
self.port_edit_apply.set_visible(true);
|
||||
self.port_edit_cancel.set_visible(true);
|
||||
}
|
||||
|
||||
#[template_callback]
|
||||
fn handle_port_edit_apply(&self) {
|
||||
self.obj().request_port_change();
|
||||
}
|
||||
|
||||
#[template_callback]
|
||||
fn handle_port_edit_cancel(&self) {
|
||||
log::debug!("cancel port edit");
|
||||
self.port_entry.set_text(self.port.get().to_string().as_str());
|
||||
self.port_edit_apply.set_visible(false);
|
||||
self.port_edit_cancel.set_visible(false);
|
||||
}
|
||||
|
||||
pub fn set_port(&self, port: u16) {
|
||||
self.port.set(port);
|
||||
if port == DEFAULT_PORT {
|
||||
self.port_entry.set_text("");
|
||||
} else {
|
||||
self.port_entry.set_text(format!("{port}").as_str());
|
||||
}
|
||||
self.port_edit_apply.set_visible(false);
|
||||
self.port_edit_cancel.set_visible(false);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -50,13 +88,14 @@ impl Window {
|
||||
impl ObjectImpl for Window {
|
||||
fn constructed(&self) {
|
||||
self.parent_constructed();
|
||||
self.set_port(DEFAULT_PORT);
|
||||
let obj = self.obj();
|
||||
obj.setup_icon();
|
||||
obj.setup_clients();
|
||||
obj.setup_callbacks();
|
||||
}
|
||||
}
|
||||
|
||||
impl WidgetImpl for Window {}
|
||||
impl WindowImpl for Window {}
|
||||
impl ApplicationWindowImpl for Window {}
|
||||
impl AdwApplicationWindowImpl for Window {}
|
||||
|
||||
Reference in New Issue
Block a user