Merge branch 'feat/closable-tabs' into 'main'

feat: make tabs closeable

Closes #17

See merge request EchidnaHQ/Echidna!9
merge-requests/11/head
Nefo Fortressia 2021-11-07 12:26:37 +00:00
commit 9569348767
5 changed files with 92 additions and 12 deletions

View File

@ -2,6 +2,8 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
use crate::lib::prelude::*;
use crate::components::editor::EchidnaCoreEditor;
use gio::Cancellable;
use glib::{clone, Priority};
@ -64,7 +66,7 @@ impl FileImplementedEditor for super::EchidnaWindow {
fn open_file(notebook: &gtk::Notebook, file_location: gio::File) {
let file = File::builder().location(&file_location).build();
let editor_page = EchidnaCoreEditor::new(Some(file));
notebook.prepend_page(
notebook.prepend_closable_page(
&editor_page,
Some(&Label::new(Some(
&file_location

View File

@ -27,16 +27,6 @@
-->
<object class="GtkNotebook" id="notebook">
<property name="hexpand">1</property>
<child>
<object id="getting-started" class="GtkLabel">
<property name="label">Hello from Echidna!</property>
</object>
</child>
<child type="tab">
<object class="GtkLabel" id="notebook-welcome">
<property name="label">Welcome</property>
</object>
</child>
</object>
<!-- </child>
</object> -->

83
src/lib/closeable_tab.rs Normal file
View File

@ -0,0 +1,83 @@
use glib::IsA;
use gtk::{prelude::*, Box, Widget, Button};
pub trait ClosableTabImplementedNotebook {
fn prepend_closable_page<
T: IsA<Widget>,
U: IsA<Widget>
>(
&self,
child: &T,
tab_label: Option<&U>,
) -> u32;
fn append_closable_page<
T: IsA<Widget>,
U: IsA<Widget>
>(
&self,
child: &T,
tab_label: Option<&U>,
) -> u32;
fn create_closable_tab<
U: IsA<Widget>,
>(tab_label: Option<&U>) -> (Box, Button);
}
impl ClosableTabImplementedNotebook for gtk::Notebook {
fn create_closable_tab<
U: IsA<Widget>,
>(tab_label: Option<&U>)-> (Box, Button) {
let tab = Box::new(gtk::Orientation::Horizontal, 5);
if tab_label.is_some() {
tab.append(tab_label.unwrap());
}
let button = gtk::Button::new();
button.set_icon_name("window-close-symbolic");
button.set_has_frame(false);
tab.append(&button);
(tab, button)
}
fn prepend_closable_page<T: IsA<Widget>, U: IsA<Widget>>(
&self,
child: &T,
tab_label: Option<&U>,
) -> u32 {
let (tab, button) = &Self::create_closable_tab(tab_label);
let page = self.prepend_page(child, Some(tab));
button.connect_clicked(glib::clone!(@weak self as notebook =>
move |_| {
notebook.remove_page(Some(page));
}));
page
}
fn append_closable_page<
T: IsA<Widget>,
U: IsA<Widget>
>(
&self,
child: &T,
tab_label: Option<&U>,
) -> u32 {
let (tab, button) = &Self::create_closable_tab(tab_label);
let page = self.append_page(child, Some(tab));
button.connect_clicked(glib::clone!(@weak self as notebook =>
move |_| {
notebook.remove_page(Some(page));
}));
page
}
}

5
src/lib/mod.rs Normal file
View File

@ -0,0 +1,5 @@
pub mod closeable_tab;
pub mod prelude {
pub use super::closeable_tab::ClosableTabImplementedNotebook;
}

View File

@ -3,7 +3,7 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
mod components;
pub mod lib;
use app::EchidnaEditor;
use components::app;
use gtk::prelude::ApplicationExtManual;