Merge branch 'main' into chore/commitizen

chore/commitizen
Nefo Fortressia 2021-11-09 05:41:54 +07:00
commit 52ef47f6fd
Signed by: fortressia
GPG Key ID: 6D7972CC76174995
6 changed files with 88 additions and 12 deletions

View File

@ -1,3 +1,7 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* 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/. */
pub mod imp;
glib::wrapper! {

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> -->

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

@ -0,0 +1,71 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* 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 glib::IsA;
use gtk::{prelude::*, Box, Button, Widget};
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
}
}

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

@ -0,0 +1,9 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* 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/. */
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;