| 1 |
// Fragments - connection_popover.rs |
| •2 |
// Copyright (C) 2022-2025 Felix Häcker <[email protected]> |
| 3 |
// |
| 4 |
// This program is free software: you can redistribute it and/or modify |
| 5 |
// it under the terms of the GNU General Public License as published by |
| 6 |
// the Free Software Foundation, either version 3 of the License, or |
| 7 |
// (at your option) any later version. |
| 8 |
// |
| 9 |
// This program is distributed in the hope that it will be useful, |
| 10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
// GNU General Public License for more details. |
| 13 |
// |
| 14 |
// You should have received a copy of the GNU General Public License |
| 15 |
// along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 16 |
|
| 17 |
use glib::{clone, subclass}; |
| 18 |
use gtk::prelude::*; |
| 19 |
use gtk::subclass::prelude::*; |
| 20 |
use gtk::{glib, CompositeTemplate}; |
| 21 |
|
| 22 |
use crate::app::FrgApplication; |
| 23 |
use crate::backend::{FrgConnection, FrgConnectionManager}; |
| 24 |
use crate::ui::FrgConnectionRow; |
| 25 |
|
| 26 |
mod imp { |
| 27 |
use super::*; |
| 28 |
|
| 29 |
#[derive(Debug, CompositeTemplate, Default)] |
| 30 |
#[template(resource = "/de/haeckerfelix/Fragments/gtk/connection_popover.ui")] |
| 31 |
pub struct FrgConnectionPopover { |
| 32 |
#[template_child] |
| 33 |
listbox: TemplateChild<gtk::ListBox>, |
| 34 |
} |
| 35 |
|
| 36 |
#[glib::object_subclass] |
| 37 |
impl ObjectSubclass for FrgConnectionPopover { |
| 38 |
const NAME: &'static str = "FrgConnectionPopover"; |
| 39 |
type ParentType = gtk::Popover; |
| 40 |
type Type = super::FrgConnectionPopover; |
| 41 |
|
| 42 |
fn class_init(klass: &mut Self::Class) { |
| 43 |
klass.bind_template(); |
| 44 |
} |
| 45 |
|
| 46 |
fn instance_init(obj: &subclass::InitializingObject<Self>) { |
| 47 |
obj.init_template(); |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
impl ObjectImpl for FrgConnectionPopover { |
| 52 |
fn constructed(&self) { |
| 53 |
self.parent_constructed(); |
| 54 |
let cm = FrgConnectionManager::default(); |
| 55 |
|
| •56 |
self.listbox.connect_row_activated(clone!( |
| 57 |
#[weak(rename_to = this)] |
| 58 |
self, |
| 59 |
move |_, row| { |
| 60 |
let row = row.downcast_ref::<FrgConnectionRow>().unwrap(); |
| 61 |
let app = FrgApplication::default(); |
| 62 |
|
| •63 |
app.activate_action( |
| 64 |
"set-connection", |
| 65 |
Some(&row.connection().uuid().to_variant()), |
| 66 |
); |
| 67 |
this.obj().popdown(); |
| •68 |
} |
| 69 |
)); |
| 70 |
|
| 71 |
self.listbox |
| 72 |
.bind_model(Some(&cm.connections()), |connection| { |
| 73 |
FrgConnectionRow::new(connection.downcast_ref::<FrgConnection>().unwrap()) |
| 74 |
.upcast() |
| 75 |
}); |
| 76 |
|
| •77 |
cm.connect_current_connection_notify(clone!( |
| 78 |
#[weak(rename_to = this)] |
| 79 |
self, |
| 80 |
move |_| { |
| 81 |
this.update_ui(); |
| 82 |
} |
| 83 |
)); |
| 84 |
|
| •85 |
cm.client().connect_is_busy_notify(clone!( |
| 86 |
#[weak(rename_to = this)] |
| 87 |
self, |
| 88 |
move |client| { |
| 89 |
// Don't allow changing the active connection |
| 90 |
// when the client is still busy establishing a connection |
| 91 |
this.obj().set_sensitive(!client.is_busy()); |
| •92 |
} |
| 93 |
)); |
| 94 |
|
| •95 |
cm.connections().connect_items_changed(clone!( |
| 96 |
#[weak(rename_to = this)] |
| 97 |
self, |
| 98 |
move |_, _, _, _| { |
| 99 |
this.update_ui(); |
| •100 |
} |
| 101 |
)); |
| 102 |
|
| 103 |
self.update_ui(); |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
impl WidgetImpl for FrgConnectionPopover {} |
| 108 |
|
| 109 |
impl PopoverImpl for FrgConnectionPopover {} |
| 110 |
|
| 111 |
impl FrgConnectionPopover { |
| 112 |
fn update_ui(&self) { |
| 113 |
let cm = FrgConnectionManager::default(); |
| 114 |
|
| 115 |
let mut i = 0; |
| 116 |
let current = cm.current_connection(); |
| 117 |
|
| 118 |
while let Some(row) = self.listbox.row_at_index(i) { |
| 119 |
let row = row.downcast::<FrgConnectionRow>().unwrap(); |
| 120 |
row.set_selected(row.connection() == current); |
| 121 |
i += 1; |
| 122 |
} |
| 123 |
} |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
glib::wrapper! { |
| 128 |
pub struct FrgConnectionPopover( |
| 129 |
ObjectSubclass<imp::FrgConnectionPopover>) |
| •130 |
@extends gtk::Widget, gtk::Popover, |
| 131 |
@implements gtk::Native, gtk::ShortcutManager, gtk::Accessible, gtk::Buildable, gtk::ConstraintTarget; |
| 132 |
} |