add hostname entry row with clipboard button

This commit is contained in:
Ferdinand Schober
2024-04-13 00:14:44 +02:00
parent 81f65dcd3d
commit 5b76c3bcda
8 changed files with 98 additions and 7 deletions

View File

@@ -12,8 +12,7 @@ use crate::frontend::gtk::window::Window;
use adw::Application;
use gtk::{
gdk::Display, glib::clone, prelude::*, subclass::prelude::ObjectSubclassIsExt,
IconTheme,
gdk::Display, glib::clone, prelude::*, subclass::prelude::ObjectSubclassIsExt, IconTheme,
};
use gtk::{gio, glib, prelude::ApplicationExt};

View File

@@ -14,6 +14,8 @@ pub struct ClientRow {
#[template_child]
pub enable_switch: TemplateChild<gtk::Switch>,
#[template_child]
pub dns_button: TemplateChild<gtk::Button>,
#[template_child]
pub hostname: TemplateChild<gtk::Entry>,
#[template_child]
pub port: TemplateChild<gtk::Entry>,
@@ -76,6 +78,11 @@ impl ClientRow {
true // dont run default handler
}
#[template_callback]
fn handle_request_dns(&self) -> bool {
false
}
#[template_callback]
fn handle_client_delete(&self, _button: &Button) {
log::debug!("delete button pressed -> requesting delete");

View File

@@ -8,7 +8,8 @@ use std::os::unix::net::UnixStream;
use adw::subclass::prelude::*;
use adw::{prelude::*, ActionRow, ToastOverlay};
use glib::subclass::InitializingObject;
use gtk::{gio, glib, Button, CompositeTemplate, Entry, ListBox};
use gtk::glib::clone;
use gtk::{gdk, gio, glib, Button, CompositeTemplate, Entry, Label, ListBox};
use crate::config::DEFAULT_PORT;
@@ -26,6 +27,8 @@ pub struct Window {
#[template_child]
pub port_entry: TemplateChild<Entry>,
#[template_child]
pub hostname_label: TemplateChild<Label>,
#[template_child]
pub toast_overlay: TemplateChild<ToastOverlay>,
pub clients: RefCell<Option<gio::ListStore>>,
#[cfg(unix)]
@@ -61,6 +64,23 @@ impl Window {
self.obj().request_client_create();
}
#[template_callback]
fn handle_copy_hostname(&self, button: &Button) {
if let Ok(hostname) = hostname::get() {
let prev_icon = button.icon_name().unwrap();
let display = gdk::Display::default().unwrap();
let clipboard = display.clipboard();
clipboard.set_text(hostname.to_str().expect("hostname: invalid utf8"));
button.set_icon_name("emblem-ok-symbolic");
button.set_css_classes(&["success"]);
glib::spawn_future_local(clone!(@weak button => async move {
glib::timeout_future_seconds(1).await;
button.set_icon_name(&prev_icon);
button.set_css_classes(&[]);
}));
}
}
#[template_callback]
fn handle_port_changed(&self, _entry: &Entry) {
self.port_edit_apply.set_visible(true);
@@ -95,6 +115,10 @@ impl Window {
impl ObjectImpl for Window {
fn constructed(&self) {
if let Ok(hostname) = hostname::get() {
self.hostname_label
.set_text(hostname.to_str().expect("hostname: invalid utf8"));
}
self.parent_constructed();
self.set_port(DEFAULT_PORT);
let obj = self.obj();