mirror of
https://github.com/YGGverse/Yoda.git
synced 2026-04-02 17:45:28 +00:00
implement columns as trait
This commit is contained in:
parent
3a8cb5673c
commit
7fff516132
2 changed files with 96 additions and 81 deletions
|
|
@ -1,3 +1,5 @@
|
||||||
|
mod column;
|
||||||
|
|
||||||
use gtk::{gio::File, ScrolledWindow};
|
use gtk::{gio::File, ScrolledWindow};
|
||||||
|
|
||||||
pub struct Directory;
|
pub struct Directory;
|
||||||
|
|
@ -6,7 +8,8 @@ impl Directory {
|
||||||
// Constructors
|
// Constructors
|
||||||
|
|
||||||
pub fn for_file(file: &File, callback: impl Fn(&File) + 'static) -> ScrolledWindow {
|
pub fn for_file(file: &File, callback: impl Fn(&File) + 'static) -> ScrolledWindow {
|
||||||
use gtk::{gio::FileInfo, Align, ListItem};
|
use column::Column;
|
||||||
|
use gtk::gio::FileInfo;
|
||||||
|
|
||||||
// Init children widget
|
// Init children widget
|
||||||
let column_view = {
|
let column_view = {
|
||||||
|
|
@ -28,86 +31,8 @@ impl Directory {
|
||||||
)
|
)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
column_view.append_column(
|
column_view.append_column(&Column::icon());
|
||||||
>k::ColumnViewColumn::builder()
|
column_view.append_column(&Column::name());
|
||||||
.title("Type")
|
|
||||||
.factory(&{
|
|
||||||
use gtk::prelude::{BoxExt, Cast, ListItemExt, WidgetExt};
|
|
||||||
let factory = gtk::SignalListItemFactory::new();
|
|
||||||
factory.connect_bind(|_, this| {
|
|
||||||
use gtk::gio::FileType;
|
|
||||||
let list_item = this.downcast_ref::<ListItem>().unwrap();
|
|
||||||
let image = gtk::Image::from_gicon(
|
|
||||||
&list_item
|
|
||||||
.item()
|
|
||||||
.unwrap()
|
|
||||||
.downcast_ref::<FileInfo>()
|
|
||||||
.unwrap()
|
|
||||||
.symbolic_icon()
|
|
||||||
.unwrap(),
|
|
||||||
);
|
|
||||||
image.set_tooltip_text(
|
|
||||||
match list_item
|
|
||||||
.item()
|
|
||||||
.unwrap()
|
|
||||||
.downcast_ref::<FileInfo>()
|
|
||||||
.unwrap()
|
|
||||||
.file_type()
|
|
||||||
{
|
|
||||||
FileType::Unknown => Some("Unknown"),
|
|
||||||
FileType::Regular => Some("File"),
|
|
||||||
FileType::Directory => Some("Directory"),
|
|
||||||
FileType::SymbolicLink => Some("SymbolicLink"),
|
|
||||||
FileType::Special => Some("Special"),
|
|
||||||
FileType::Shortcut => Some("Shortcut"),
|
|
||||||
FileType::Mountable => Some("Mountable"),
|
|
||||||
_ => None,
|
|
||||||
},
|
|
||||||
);
|
|
||||||
let container = gtk::Box::builder().halign(Align::Center).build(); // prevents `gtk::Image` blur
|
|
||||||
container.append(&image);
|
|
||||||
list_item.set_child(Some(&container));
|
|
||||||
});
|
|
||||||
factory
|
|
||||||
})
|
|
||||||
.build(),
|
|
||||||
);
|
|
||||||
|
|
||||||
column_view.append_column(
|
|
||||||
>k::ColumnViewColumn::builder()
|
|
||||||
.expand(true)
|
|
||||||
.title("Name")
|
|
||||||
.factory(&{
|
|
||||||
let factory = gtk::SignalListItemFactory::new();
|
|
||||||
factory.connect_bind(|_, this| {
|
|
||||||
use gtk::prelude::{Cast, /*FileExt,*/ ListItemExt};
|
|
||||||
let list_item = this.downcast_ref::<ListItem>().unwrap();
|
|
||||||
let item = list_item.item().unwrap();
|
|
||||||
let file_info = item.downcast_ref::<FileInfo>().unwrap();
|
|
||||||
list_item.set_child(Some(
|
|
||||||
>k::Label::builder()
|
|
||||||
.halign(Align::Start)
|
|
||||||
.ellipsize(gtk::pango::EllipsizeMode::Middle)
|
|
||||||
.label(file_info.display_name())
|
|
||||||
/*.tooltip_text(
|
|
||||||
file_info
|
|
||||||
.attribute_object("standard::file")
|
|
||||||
.unwrap()
|
|
||||||
.downcast_ref::<File>()
|
|
||||||
.unwrap()
|
|
||||||
.path()
|
|
||||||
.unwrap()
|
|
||||||
.to_str()
|
|
||||||
.unwrap(),
|
|
||||||
) this feature maybe is not really wanted */
|
|
||||||
.build(),
|
|
||||||
));
|
|
||||||
});
|
|
||||||
factory
|
|
||||||
})
|
|
||||||
.build(),
|
|
||||||
);
|
|
||||||
|
|
||||||
column_view
|
column_view
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,90 @@
|
||||||
|
pub trait Column {
|
||||||
|
fn icon() -> Self;
|
||||||
|
fn name() -> Self;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Column for gtk::ColumnViewColumn {
|
||||||
|
fn icon() -> Self {
|
||||||
|
use gtk::{
|
||||||
|
gio::{FileInfo, FileType},
|
||||||
|
prelude::{BoxExt, Cast, ListItemExt, WidgetExt},
|
||||||
|
Align, ColumnViewColumn, ListItem, SignalListItemFactory,
|
||||||
|
};
|
||||||
|
|
||||||
|
ColumnViewColumn::builder()
|
||||||
|
.title("Type")
|
||||||
|
.factory(&{
|
||||||
|
let factory = SignalListItemFactory::new();
|
||||||
|
factory.connect_bind(|_, this| {
|
||||||
|
let list_item = this.downcast_ref::<ListItem>().unwrap();
|
||||||
|
let image = gtk::Image::from_gicon(
|
||||||
|
&list_item
|
||||||
|
.item()
|
||||||
|
.unwrap()
|
||||||
|
.downcast_ref::<FileInfo>()
|
||||||
|
.unwrap()
|
||||||
|
.symbolic_icon()
|
||||||
|
.unwrap(),
|
||||||
|
);
|
||||||
|
image.set_tooltip_text(
|
||||||
|
match list_item
|
||||||
|
.item()
|
||||||
|
.unwrap()
|
||||||
|
.downcast_ref::<FileInfo>()
|
||||||
|
.unwrap()
|
||||||
|
.file_type()
|
||||||
|
{
|
||||||
|
FileType::Unknown => Some("Unknown"),
|
||||||
|
FileType::Regular => Some("File"),
|
||||||
|
FileType::Directory => Some("Directory"),
|
||||||
|
FileType::SymbolicLink => Some("SymbolicLink"),
|
||||||
|
FileType::Special => Some("Special"),
|
||||||
|
FileType::Shortcut => Some("Shortcut"),
|
||||||
|
FileType::Mountable => Some("Mountable"),
|
||||||
|
_ => None,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
let container = gtk::Box::builder().halign(Align::Center).build(); // prevents `gtk::Image` blur
|
||||||
|
container.append(&image);
|
||||||
|
list_item.set_child(Some(&container));
|
||||||
|
});
|
||||||
|
factory
|
||||||
|
})
|
||||||
|
.build()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn name() -> Self {
|
||||||
|
gtk::ColumnViewColumn::builder()
|
||||||
|
.expand(true)
|
||||||
|
.title("Name")
|
||||||
|
.factory(&{
|
||||||
|
let factory = gtk::SignalListItemFactory::new();
|
||||||
|
factory.connect_bind(|_, this| {
|
||||||
|
use gtk::prelude::{Cast, /*FileExt,*/ ListItemExt};
|
||||||
|
let list_item = this.downcast_ref::<gtk::ListItem>().unwrap();
|
||||||
|
let item = list_item.item().unwrap();
|
||||||
|
let file_info = item.downcast_ref::<gtk::gio::FileInfo>().unwrap();
|
||||||
|
list_item.set_child(Some(
|
||||||
|
>k::Label::builder()
|
||||||
|
.halign(gtk::Align::Start)
|
||||||
|
.ellipsize(gtk::pango::EllipsizeMode::Middle)
|
||||||
|
.label(file_info.display_name())
|
||||||
|
/*.tooltip_text(
|
||||||
|
file_info
|
||||||
|
.attribute_object("standard::file")
|
||||||
|
.unwrap()
|
||||||
|
.downcast_ref::<File>()
|
||||||
|
.unwrap()
|
||||||
|
.path()
|
||||||
|
.unwrap()
|
||||||
|
.to_str()
|
||||||
|
.unwrap(),
|
||||||
|
) this feature maybe is not really wanted */
|
||||||
|
.build(),
|
||||||
|
));
|
||||||
|
});
|
||||||
|
factory
|
||||||
|
})
|
||||||
|
.build()
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue