| 1 |
// Fragments - connection_popover.rs |
| 2 |
// Copyright (C) 2022-2023 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 |
| 57 |
.connect_row_activated(clone!(@weak self as this => move |_, row|{ |
| 58 |
let row = row.downcast_ref::<FrgConnectionRow>().unwrap(); |
| 59 |
let app = FrgApplication::default(); |
| 60 |
|
| 61 |
app.activate_action("set-connection", Some(&row.connection().uuid().to_variant())); |
| 62 |
this.obj().popdown(); |
| 63 |
})); |
| 64 |
|
| 65 |
self.listbox |
| 66 |
.bind_model(Some(&cm.connections()), |connection| { |
| 67 |
FrgConnectionRow::new(connection.downcast_ref::<FrgConnection>().unwrap()) |
| 68 |
.upcast() |
| 69 |
}); |
| 70 |
|
| 71 |
cm.connect_current_connection_notify(clone!(@weak self as this => move |_|{ |
| 72 |
this.update_ui(); |
| 73 |
})); |
| 74 |
|
| 75 |
cm.client() |
| 76 |
.connect_is_busy_notify(clone!(@weak self as this => move |client|{ |
| 77 |
// Don't allow changing the active connection |
| 78 |
// when the client is still busy establishing a connection |
| 79 |
this.obj().set_sensitive(!client.is_busy()); |
| 80 |
})); |
| 81 |
|
| 82 |
cm.connections() |
| 83 |
.connect_items_changed(clone!(@weak self as this => move |_,_,_,_|{ |
| 84 |
this.update_ui(); |
| 85 |
})); |
| 86 |
|
| 87 |
self.update_ui(); |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
impl WidgetImpl for FrgConnectionPopover {} |
| 92 |
|
| 93 |
impl PopoverImpl for FrgConnectionPopover {} |
| 94 |
|
| 95 |
impl FrgConnectionPopover { |
| 96 |
fn update_ui(&self) { |
| 97 |
let cm = FrgConnectionManager::default(); |
| 98 |
|
| 99 |
let mut i = 0; |
| 100 |
let current = cm.current_connection(); |
| 101 |
|
| 102 |
while let Some(row) = self.listbox.row_at_index(i) { |
| 103 |
let row = row.downcast::<FrgConnectionRow>().unwrap(); |
| 104 |
row.set_selected(row.connection() == current); |
| 105 |
i += 1; |
| 106 |
} |
| 107 |
} |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
glib::wrapper! { |
| 112 |
pub struct FrgConnectionPopover( |
| 113 |
ObjectSubclass<imp::FrgConnectionPopover>) |
| 114 |
@extends gtk::Widget, gtk::Popover; |
| 115 |
} |