feat: add graphical interface with relm4 and libadw
This commit is contained in:
parent
837ce3ab3e
commit
601ee85b5f
6 changed files with 850 additions and 29 deletions
80
src/gobject.rs
Normal file
80
src/gobject.rs
Normal file
|
@ -0,0 +1,80 @@
|
|||
use std::{cell::Cell, sync::LazyLock};
|
||||
|
||||
use relm4::{
|
||||
adw::subclass::prelude::ObjectSubclassType,
|
||||
gtk::{
|
||||
glib::{self, ParamSpecString, Type, object::ObjectExt, value::ToValue},
|
||||
subclass::prelude::{ObjectImpl, ObjectSubclass},
|
||||
},
|
||||
};
|
||||
|
||||
use crate::zipline::ZiplineFolder;
|
||||
|
||||
glib::wrapper! {
|
||||
pub struct GtkZiplineFolder(ObjectSubclass<GtkZiplineFolderImpl>);
|
||||
}
|
||||
|
||||
impl GtkZiplineFolder {
|
||||
pub fn r#type() -> Type {
|
||||
GtkZiplineFolderImpl::type_()
|
||||
}
|
||||
|
||||
pub fn from_folder(folder: &ZiplineFolder) -> Self {
|
||||
glib::Object::builder()
|
||||
.property("id", folder.id.clone())
|
||||
.property("name", folder.name.clone())
|
||||
.build()
|
||||
}
|
||||
|
||||
pub fn as_folder(&self) -> ZiplineFolder {
|
||||
ZiplineFolder {
|
||||
id: self.property("id"),
|
||||
name: self.property("name"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct GtkZiplineFolderImpl {
|
||||
id: Cell<String>,
|
||||
name: Cell<String>,
|
||||
}
|
||||
|
||||
#[glib::object_subclass]
|
||||
impl ObjectSubclass for GtkZiplineFolderImpl {
|
||||
const NAME: &str = "TyrolienneZiplineFolder";
|
||||
type Type = GtkZiplineFolder;
|
||||
}
|
||||
|
||||
impl ObjectImpl for GtkZiplineFolderImpl {
|
||||
fn properties() -> &'static [glib::ParamSpec] {
|
||||
static PARAMS: LazyLock<[glib::ParamSpec; 2]> = LazyLock::new(|| {
|
||||
[
|
||||
ParamSpecString::builder("id").build(),
|
||||
ParamSpecString::builder("name").build(),
|
||||
]
|
||||
});
|
||||
|
||||
PARAMS.as_ref()
|
||||
}
|
||||
|
||||
fn set_property(&self, _id: usize, value: &glib::Value, pspec: &glib::ParamSpec) {
|
||||
let value = value.get().unwrap();
|
||||
|
||||
match pspec.name() {
|
||||
"id" => self.id.replace(value),
|
||||
"name" => self.name.replace(value),
|
||||
_ => unreachable!(),
|
||||
};
|
||||
}
|
||||
|
||||
fn property(&self, _id: usize, pspec: &glib::ParamSpec) -> glib::Value {
|
||||
unsafe {
|
||||
match pspec.name() {
|
||||
"id" => (*self.id.as_ptr()).to_value(),
|
||||
"name" => (*self.name.as_ptr()).to_value(),
|
||||
_ => unreachable!(),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
213
src/main.rs
213
src/main.rs
|
@ -1,11 +1,19 @@
|
|||
mod gobject;
|
||||
mod zipline;
|
||||
|
||||
use std::{path::Path, time::Duration};
|
||||
use std::{borrow::Cow, path::PathBuf, time::Duration};
|
||||
|
||||
use color_eyre::eyre::{OptionExt, Result, bail};
|
||||
use gobject::GtkZiplineFolder;
|
||||
use relm4::{
|
||||
ComponentParts, RelmApp, SimpleComponent,
|
||||
adw::{self, prelude::*},
|
||||
gtk::{self, gio, glib::clone},
|
||||
};
|
||||
use tracing::Level;
|
||||
use tracing_subscriber::EnvFilter;
|
||||
use urlencoding::encode;
|
||||
use urlencoding::Encoded;
|
||||
use zipline::ZiplineFolder;
|
||||
|
||||
#[derive(Debug, Default, serde::Deserialize, serde::Serialize)]
|
||||
struct Config {
|
||||
|
@ -27,6 +35,172 @@ impl Config {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
enum Message {
|
||||
SetPath(PathBuf),
|
||||
SetFolder(ZiplineFolder),
|
||||
StartTheProcess,
|
||||
}
|
||||
|
||||
struct Widgets {
|
||||
file_picker_row: adw::ActionRow,
|
||||
send_button: gtk::Button,
|
||||
}
|
||||
|
||||
struct Tyrolienne {
|
||||
config: Config,
|
||||
video_path: Option<PathBuf>,
|
||||
folder: ZiplineFolder,
|
||||
}
|
||||
|
||||
impl Tyrolienne {
|
||||
fn display_video_path(&self) -> Cow<'_, str> {
|
||||
self.video_path
|
||||
.as_ref()
|
||||
.map(|p| p.to_string_lossy())
|
||||
.unwrap_or(Cow::Borrowed("None"))
|
||||
}
|
||||
}
|
||||
|
||||
impl SimpleComponent for Tyrolienne {
|
||||
type Input = Message;
|
||||
type Output = ();
|
||||
type Init = (Config, Vec<ZiplineFolder>);
|
||||
type Root = adw::ApplicationWindow;
|
||||
type Widgets = Widgets;
|
||||
|
||||
fn init_root() -> Self::Root {
|
||||
adw::ApplicationWindow::builder()
|
||||
.title("Tyrolienne")
|
||||
.default_width(500)
|
||||
.build()
|
||||
}
|
||||
|
||||
fn init(
|
||||
(config, folders): Self::Init,
|
||||
root: Self::Root,
|
||||
sender: relm4::ComponentSender<Self>,
|
||||
) -> relm4::ComponentParts<Self> {
|
||||
let model = Tyrolienne {
|
||||
config,
|
||||
video_path: None,
|
||||
folder: folders[0].clone(),
|
||||
};
|
||||
|
||||
let file_picker_row = adw::ActionRow::builder()
|
||||
.activatable(true)
|
||||
.title("Video file")
|
||||
.subtitle(model.display_video_path())
|
||||
.css_classes(["property"])
|
||||
.build();
|
||||
file_picker_row.connect_activated(clone!(
|
||||
#[strong]
|
||||
sender,
|
||||
#[strong]
|
||||
root,
|
||||
move |_| {
|
||||
let file_dialog = gtk::FileDialog::new();
|
||||
file_dialog.open(
|
||||
Some(&root),
|
||||
gio::Cancellable::NONE,
|
||||
clone!(
|
||||
#[strong]
|
||||
sender,
|
||||
move |file| {
|
||||
if let Some(path) = file.ok().and_then(|f| f.path()) {
|
||||
sender.input(Message::SetPath(path));
|
||||
}
|
||||
}
|
||||
),
|
||||
);
|
||||
}
|
||||
));
|
||||
|
||||
let gtk_folders = folders
|
||||
.iter()
|
||||
.map(GtkZiplineFolder::from_folder)
|
||||
.collect::<Vec<_>>();
|
||||
let store = gio::ListStore::new::<GtkZiplineFolder>();
|
||||
store.extend_from_slice(>k_folders);
|
||||
|
||||
let folder_row = adw::ComboRow::builder()
|
||||
.title("Folder")
|
||||
.model(&store)
|
||||
.expression(gtk::PropertyExpression::new(
|
||||
GtkZiplineFolder::r#type(),
|
||||
None::<>k::Expression>,
|
||||
"name",
|
||||
))
|
||||
.build();
|
||||
folder_row.connect_activated(clone!(
|
||||
#[strong]
|
||||
sender,
|
||||
move |r| {
|
||||
if let Some(item) = r
|
||||
.selected_item()
|
||||
.and_then(|i| i.downcast::<GtkZiplineFolder>().ok())
|
||||
{
|
||||
sender.input(Message::SetFolder(item.as_folder()));
|
||||
}
|
||||
}
|
||||
));
|
||||
|
||||
let send_button = gtk::Button::builder()
|
||||
.label("Send")
|
||||
.sensitive(false)
|
||||
.margin_start(32)
|
||||
.margin_end(32)
|
||||
.margin_bottom(32)
|
||||
.build();
|
||||
send_button.connect_clicked(clone!(
|
||||
#[strong]
|
||||
sender,
|
||||
move |_| sender.input(Message::StartTheProcess)
|
||||
));
|
||||
|
||||
let list = gtk::ListBox::builder()
|
||||
.margin_top(32)
|
||||
.margin_end(32)
|
||||
.margin_bottom(32)
|
||||
.margin_start(32)
|
||||
.selection_mode(gtk::SelectionMode::None)
|
||||
.css_classes(["boxed-list"])
|
||||
.build();
|
||||
list.append(&file_picker_row);
|
||||
list.append(&folder_row);
|
||||
|
||||
let root_box = gtk::Box::new(gtk::Orientation::Vertical, 0);
|
||||
root_box.append(&adw::HeaderBar::new());
|
||||
root_box.append(&list);
|
||||
root_box.append(&send_button);
|
||||
|
||||
root.set_content(Some(&root_box));
|
||||
|
||||
let widgets = Widgets {
|
||||
file_picker_row,
|
||||
send_button,
|
||||
};
|
||||
|
||||
ComponentParts { model, widgets }
|
||||
}
|
||||
|
||||
fn update(&mut self, message: Self::Input, _sender: relm4::ComponentSender<Self>) {
|
||||
match message {
|
||||
Message::SetPath(path) => self.video_path = Some(path),
|
||||
Message::SetFolder(folder) => self.folder = folder,
|
||||
Message::StartTheProcess => the_process(self).unwrap(),
|
||||
}
|
||||
}
|
||||
|
||||
fn update_view(&self, widgets: &mut Self::Widgets, _sender: relm4::ComponentSender<Self>) {
|
||||
widgets
|
||||
.file_picker_row
|
||||
.set_subtitle(&self.display_video_path());
|
||||
|
||||
widgets.send_button.set_sensitive(self.video_path.is_some());
|
||||
}
|
||||
}
|
||||
|
||||
fn main() -> Result<()> {
|
||||
tracing_subscriber::fmt()
|
||||
.with_env_filter(EnvFilter::from_default_env().add_directive(Level::INFO.into()))
|
||||
|
@ -34,6 +208,17 @@ fn main() -> Result<()> {
|
|||
|
||||
color_eyre::install()?;
|
||||
|
||||
// TODO: show dialog in case these error
|
||||
let config = get_config()?;
|
||||
let folders = zipline::get_folders(&config)?;
|
||||
|
||||
let app = RelmApp::new("net.uku3lig.Tyrolienne");
|
||||
app.run::<Tyrolienne>((config, folders));
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn get_config() -> Result<Config> {
|
||||
let Some(config_dir) = dirs::config_dir() else {
|
||||
bail!("could not get config dir!");
|
||||
};
|
||||
|
@ -59,37 +244,33 @@ fn main() -> Result<()> {
|
|||
bail!("zipline url is empty! please set it at {config_file_path:?}");
|
||||
}
|
||||
|
||||
let args: Vec<String> = std::env::args().collect();
|
||||
let file = args.get(1).ok_or_eyre(
|
||||
"please specify the path to the file you want to upload as the first command line argument",
|
||||
)?;
|
||||
Ok(config)
|
||||
}
|
||||
|
||||
let folders = zipline::get_folders(&config)?;
|
||||
let folder = &folders[0];
|
||||
fn the_process(app: &Tyrolienne) -> Result<()> {
|
||||
tracing::info!("uploading to folder '{}'...", app.folder.name);
|
||||
|
||||
tracing::info!("uploading to folder '{}'...", folder.name);
|
||||
|
||||
let res = zipline::upload_file(&config, folder, Path::new(file))?;
|
||||
let res = zipline::upload_file(&app.config, &app.folder, app.video_path.as_ref().unwrap())?;
|
||||
let zp_file = &res.files[0];
|
||||
|
||||
tracing::info!("recalculating thumbnails...");
|
||||
|
||||
zipline::recalc_thumbnails(&config)?;
|
||||
zipline::recalc_thumbnails(&app.config)?;
|
||||
|
||||
std::thread::sleep(Duration::from_secs(5));
|
||||
|
||||
tracing::info!("fetching thumbnail url...");
|
||||
|
||||
let res = zipline::get_file_details(&config, &zp_file.id)?;
|
||||
let res = zipline::get_file_details(&app.config, &zp_file.id)?;
|
||||
let thumbnail_url = res
|
||||
.thumbnail_url(&config)
|
||||
.thumbnail_url(&app.config)
|
||||
.ok_or_eyre("could not get thumbnail url")?;
|
||||
|
||||
// TODO get w&h from video
|
||||
let autocomp_url = format!(
|
||||
"https://autocompressor.net/av1?v={}&i={}&w=1920&h=1080",
|
||||
encode(&zp_file.url),
|
||||
encode(&thumbnail_url)
|
||||
Encoded(&zp_file.url),
|
||||
Encoded(&thumbnail_url)
|
||||
);
|
||||
tracing::info!("url: {autocomp_url}");
|
||||
|
||||
|
|
|
@ -9,7 +9,7 @@ use crate::Config;
|
|||
|
||||
static CLIENT: LazyLock<Client> = LazyLock::new(Client::new);
|
||||
|
||||
#[derive(Debug, serde::Deserialize)]
|
||||
#[derive(Debug, Clone, serde::Deserialize)]
|
||||
pub struct ZiplineFolder {
|
||||
pub id: String,
|
||||
pub name: String,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue