|
|
@@ -6,17 +6,22 @@ mod desktop_install;
|
|
|
|
|
|
use std::path::PathBuf;
|
|
|
|
|
|
+use desktop_install::LauncherIcon;
|
|
|
+
|
|
|
use clap::{Parser, Subcommand};
|
|
|
use libadwaita as adw;
|
|
|
|
|
|
use adw::prelude::*;
|
|
|
-use adw::{Application, ApplicationWindow};
|
|
|
+use adw::{Application, ApplicationWindow, PreferencesGroup};
|
|
|
use gtk4::gio::ApplicationFlags;
|
|
|
use gtk4::gio::prelude::ApplicationExtManual;
|
|
|
use gtk4::glib;
|
|
|
use gtk4::glib::prelude::{Cast, IsA, ObjectExt};
|
|
|
-use gtk4::prelude::{NativeExt, WidgetExt};
|
|
|
-use gtk4::{Align, Overlay, PackType, WindowControls};
|
|
|
+use gtk4::prelude::{GtkWindowExt, NativeExt, WidgetExt};
|
|
|
+use gtk4::{
|
|
|
+ Align, Box as GtkBox, Button, Entry, Image, ListBox, Orientation, Overlay, PackType,
|
|
|
+ ScrolledWindow, SelectionMode, WindowControls,
|
|
|
+};
|
|
|
use gtk4::gdk::prelude::{DeviceExt, SeatExt, ToplevelExt};
|
|
|
use gtk4::gdk;
|
|
|
use webkit6::prelude::*;
|
|
|
@@ -253,30 +258,239 @@ fn build_persistent_network_session() -> NetworkSession {
|
|
|
session
|
|
|
}
|
|
|
|
|
|
-fn load_uri_into_overlay_webview(win: &ApplicationWindow, url: &str) {
|
|
|
- let Some(shell) = win.content() else {
|
|
|
- return;
|
|
|
- };
|
|
|
- let Some(overlay) = shell.downcast_ref::<Overlay>() else {
|
|
|
- return;
|
|
|
+fn open_web_window(app: &Application, url: &str) {
|
|
|
+ let window = ApplicationWindow::builder()
|
|
|
+ .application(app)
|
|
|
+ .title("Appiphany")
|
|
|
+ .default_width(1024)
|
|
|
+ .default_height(768)
|
|
|
+ .build();
|
|
|
+
|
|
|
+ let network_session = network_session_for_app(app);
|
|
|
+ let webview = build_appiphany_webview(&window, &network_session);
|
|
|
+ let shell = build_overlay_with_webview(&webview);
|
|
|
+ window.set_content(Some(&shell));
|
|
|
+ webview.load_uri(url);
|
|
|
+ window.present();
|
|
|
+}
|
|
|
+
|
|
|
+fn launcher_icon_image(icon: &LauncherIcon) -> Image {
|
|
|
+ let img = match icon {
|
|
|
+ LauncherIcon::File(path) => Image::from_file(path),
|
|
|
+ LauncherIcon::Themed(name) => Image::from_icon_name(name),
|
|
|
};
|
|
|
+ img.set_pixel_size(32);
|
|
|
+ img.set_valign(Align::Center);
|
|
|
+ img
|
|
|
+}
|
|
|
+
|
|
|
+fn clear_listbox_rows(list: &ListBox) {
|
|
|
+ while let Some(row) = list.row_at_index(0) {
|
|
|
+ list.remove(&row);
|
|
|
+ }
|
|
|
+}
|
|
|
|
|
|
- if let Some(child) = overlay.child() {
|
|
|
- if let Ok(webview) = child.downcast::<WebView>() {
|
|
|
- webview.load_uri(url);
|
|
|
- return;
|
|
|
+fn refresh_launchers_list(
|
|
|
+ list: &ListBox,
|
|
|
+ app: &Application,
|
|
|
+ manager_window: &ApplicationWindow,
|
|
|
+) {
|
|
|
+ clear_listbox_rows(list);
|
|
|
+ match desktop_install::list_launchers() {
|
|
|
+ Ok(launchers) => {
|
|
|
+ if launchers.is_empty() {
|
|
|
+ let row = adw::ActionRow::builder()
|
|
|
+ .title("No launchers found")
|
|
|
+ .activatable(false)
|
|
|
+ .selectable(false)
|
|
|
+ .build();
|
|
|
+ row.add_prefix(&launcher_icon_image(&LauncherIcon::Themed(
|
|
|
+ "applications-internet".to_string(),
|
|
|
+ )));
|
|
|
+ list.append(&row);
|
|
|
+ } else {
|
|
|
+ for launcher in launchers {
|
|
|
+ let (title, subtitle) = if let Some(url) = &launcher.url {
|
|
|
+ (
|
|
|
+ launcher
|
|
|
+ .name
|
|
|
+ .as_deref()
|
|
|
+ .unwrap_or("Unnamed")
|
|
|
+ .to_string(),
|
|
|
+ url.clone(),
|
|
|
+ )
|
|
|
+ } else {
|
|
|
+ (
|
|
|
+ launcher
|
|
|
+ .name
|
|
|
+ .unwrap_or_else(|| "Unnamed".to_string()),
|
|
|
+ format!("ID {}", launcher.id),
|
|
|
+ )
|
|
|
+ };
|
|
|
+
|
|
|
+ let row = adw::ActionRow::builder()
|
|
|
+ .title(&title)
|
|
|
+ .subtitle(&subtitle)
|
|
|
+ .subtitle_lines(2)
|
|
|
+ .activatable(false)
|
|
|
+ .selectable(false)
|
|
|
+ .build();
|
|
|
+
|
|
|
+ row.add_prefix(&launcher_icon_image(&launcher.icon));
|
|
|
+
|
|
|
+ let actions = GtkBox::new(Orientation::Horizontal, 6);
|
|
|
+ actions.set_valign(Align::Center);
|
|
|
+
|
|
|
+ let open_btn = Button::with_label("Open");
|
|
|
+ open_btn.add_css_class("flat");
|
|
|
+ open_btn.set_valign(Align::Center);
|
|
|
+ let uninstall_btn = Button::with_label("Unregister");
|
|
|
+ uninstall_btn.add_css_class("flat");
|
|
|
+ uninstall_btn.set_valign(Align::Center);
|
|
|
+
|
|
|
+ let id = launcher.id.clone();
|
|
|
+ let url = launcher.url.clone();
|
|
|
+ let app_open = app.clone();
|
|
|
+ open_btn.connect_clicked(move |_| {
|
|
|
+ if let Some(url) = &url {
|
|
|
+ open_web_window(&app_open, url);
|
|
|
+ } else {
|
|
|
+ eprintln!("appiphany: launcher has no URL in Exec entry");
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ let list_uninstall = list.clone();
|
|
|
+ let app_uninstall = app.clone();
|
|
|
+ let manager_window_uninstall = manager_window.clone();
|
|
|
+ uninstall_btn.connect_clicked(move |_| {
|
|
|
+ let dialog = adw::AlertDialog::new(
|
|
|
+ Some("Unregister launcher?"),
|
|
|
+ Some("This will remove the launcher entry and stored icons."),
|
|
|
+ );
|
|
|
+ dialog.add_response("cancel", "Cancel");
|
|
|
+ dialog.add_response("unregister", "Unregister");
|
|
|
+ dialog.set_close_response("cancel");
|
|
|
+ dialog.set_default_response(Some("cancel"));
|
|
|
+ dialog.set_response_appearance("unregister", adw::ResponseAppearance::Destructive);
|
|
|
+
|
|
|
+ let id = id.clone();
|
|
|
+ let list_uninstall = list_uninstall.clone();
|
|
|
+ let app_uninstall = app_uninstall.clone();
|
|
|
+ let manager_window_for_response = manager_window_uninstall.clone();
|
|
|
+ dialog.connect_response(Some("unregister"), move |_, _| {
|
|
|
+ match desktop_install::run_uninstall(&id, normalize_url) {
|
|
|
+ Ok(()) => refresh_launchers_list(
|
|
|
+ &list_uninstall,
|
|
|
+ &app_uninstall,
|
|
|
+ &manager_window_for_response,
|
|
|
+ ),
|
|
|
+ Err(e) => eprintln!("appiphany: unregister failed: {e}"),
|
|
|
+ }
|
|
|
+ });
|
|
|
+ dialog.present(Some(&manager_window_uninstall));
|
|
|
+ });
|
|
|
+
|
|
|
+ actions.append(&open_btn);
|
|
|
+ actions.append(&uninstall_btn);
|
|
|
+ row.add_suffix(&actions);
|
|
|
+
|
|
|
+ list.append(&row);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ Err(e) => {
|
|
|
+ eprintln!("appiphany: failed to list launchers: {e}");
|
|
|
}
|
|
|
}
|
|
|
+}
|
|
|
|
|
|
- let Some(app) = win.application() else {
|
|
|
- return;
|
|
|
- };
|
|
|
- let session = network_session_for_app(&app);
|
|
|
- let webview = build_appiphany_webview(win, &session);
|
|
|
- webview.set_hexpand(true);
|
|
|
- webview.set_vexpand(true);
|
|
|
- overlay.set_child(Some(&webview));
|
|
|
- webview.load_uri(url);
|
|
|
+fn build_manager_window(app: &Application) {
|
|
|
+ let header = adw::HeaderBar::new();
|
|
|
+ let window_title = adw::WindowTitle::new("Appiphany", "Run webapps as native windows");
|
|
|
+ header.set_title_widget(Some(&window_title));
|
|
|
+ header.set_show_end_title_buttons(true);
|
|
|
+ header.set_show_start_title_buttons(false);
|
|
|
+
|
|
|
+ let window = ApplicationWindow::builder()
|
|
|
+ .application(app)
|
|
|
+ .title("Appiphany")
|
|
|
+ .default_width(900)
|
|
|
+ .default_height(640)
|
|
|
+ .build();
|
|
|
+
|
|
|
+ window.set_resizable(true);
|
|
|
+ window.set_size_request(420, 320);
|
|
|
+
|
|
|
+ let root = GtkBox::new(Orientation::Vertical, 12);
|
|
|
+ root.set_margin_top(12);
|
|
|
+ root.set_margin_bottom(12);
|
|
|
+ root.set_margin_start(12);
|
|
|
+ root.set_margin_end(12);
|
|
|
+
|
|
|
+ let open_row = GtkBox::new(Orientation::Horizontal, 8);
|
|
|
+ let url_entry = Entry::new();
|
|
|
+ url_entry.set_hexpand(true);
|
|
|
+ url_entry.set_placeholder_text(Some("https://example.com"));
|
|
|
+ let open_button = Button::with_label("Open");
|
|
|
+ let create_button = Button::with_label("Create Launcher");
|
|
|
+ open_row.append(&url_entry);
|
|
|
+ open_row.append(&open_button);
|
|
|
+ open_row.append(&create_button);
|
|
|
+
|
|
|
+ let launchers_group = PreferencesGroup::builder()
|
|
|
+ .title("Installed launchers")
|
|
|
+ .build();
|
|
|
+ let list = ListBox::new();
|
|
|
+ list.add_css_class("boxed-list");
|
|
|
+ list.set_selection_mode(SelectionMode::None);
|
|
|
+ launchers_group.add(&list);
|
|
|
+ let list_scroll = ScrolledWindow::builder()
|
|
|
+ .vexpand(true)
|
|
|
+ .hexpand(true)
|
|
|
+ .child(&launchers_group)
|
|
|
+ .build();
|
|
|
+ let app_open = app.clone();
|
|
|
+ let entry_open = url_entry.clone();
|
|
|
+ open_button.connect_clicked(move |_| {
|
|
|
+ let raw = entry_open.text().to_string();
|
|
|
+ match normalize_url(&raw) {
|
|
|
+ Ok(url) => open_web_window(&app_open, &url),
|
|
|
+ Err(msg) => eprintln!("appiphany: {msg}"),
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ let entry_create = url_entry.clone();
|
|
|
+ let list_create = list.clone();
|
|
|
+ let app_create = app.clone();
|
|
|
+ let manager_window_create = window.clone();
|
|
|
+ create_button.connect_clicked(move |_| {
|
|
|
+ let raw = entry_create.text().to_string();
|
|
|
+ match normalize_url(&raw) {
|
|
|
+ Ok(url) => match desktop_install::run_install(&url, None, true, normalize_url) {
|
|
|
+ Ok(()) => refresh_launchers_list(
|
|
|
+ &list_create,
|
|
|
+ &app_create,
|
|
|
+ &manager_window_create,
|
|
|
+ ),
|
|
|
+ Err(e) => eprintln!("appiphany: create launcher failed: {e}"),
|
|
|
+ },
|
|
|
+ Err(msg) => eprintln!("appiphany: {msg}"),
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ root.append(&open_row);
|
|
|
+ root.append(&list_scroll);
|
|
|
+
|
|
|
+ root.set_hexpand(true);
|
|
|
+ root.set_vexpand(true);
|
|
|
+
|
|
|
+ let shell = adw::ToolbarView::new();
|
|
|
+ shell.add_top_bar(&header);
|
|
|
+ shell.set_content(Some(&root));
|
|
|
+
|
|
|
+ window.set_content(Some(&shell));
|
|
|
+ refresh_launchers_list(&list, app, &window);
|
|
|
+ window.present();
|
|
|
}
|
|
|
|
|
|
fn normalize_url(arg: &str) -> Result<String, &'static str> {
|
|
|
@@ -348,10 +562,7 @@ fn main() -> glib::ExitCode {
|
|
|
eprintln!("appiphany: cannot pass a URL together with a subcommand");
|
|
|
return glib::ExitCode::from(2);
|
|
|
}
|
|
|
- (None, None) => {
|
|
|
- eprintln!("appiphany: expected <URL> or a subcommand (see --help)");
|
|
|
- return glib::ExitCode::from(2);
|
|
|
- }
|
|
|
+ (None, None) => {}
|
|
|
_ => {}
|
|
|
}
|
|
|
|
|
|
@@ -373,9 +584,11 @@ fn main() -> glib::ExitCode {
|
|
|
};
|
|
|
}
|
|
|
|
|
|
- if let Err(msg) = normalize_url(parsed.url.as_ref().expect("validated")) {
|
|
|
- eprintln!("appiphany: {msg}");
|
|
|
- return glib::ExitCode::from(2);
|
|
|
+ if let Some(url) = parsed.url.as_deref() {
|
|
|
+ if let Err(msg) = normalize_url(url) {
|
|
|
+ eprintln!("appiphany: {msg}");
|
|
|
+ return glib::ExitCode::from(2);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
if let Err(e) = adw::init() {
|
|
|
@@ -402,8 +615,16 @@ fn main() -> glib::ExitCode {
|
|
|
}
|
|
|
};
|
|
|
|
|
|
+ if args.len() == 1 {
|
|
|
+ if app.active_window().is_none() {
|
|
|
+ build_manager_window(app);
|
|
|
+ } else if let Some(win) = app.active_window() {
|
|
|
+ win.present();
|
|
|
+ }
|
|
|
+ return glib::ExitCode::SUCCESS;
|
|
|
+ }
|
|
|
if args.len() != 2 {
|
|
|
- eprintln!("Usage: appiphany <url>");
|
|
|
+ eprintln!("Usage: appiphany [url]");
|
|
|
return glib::ExitCode::from(2);
|
|
|
}
|
|
|
|
|
|
@@ -415,27 +636,7 @@ fn main() -> glib::ExitCode {
|
|
|
}
|
|
|
};
|
|
|
|
|
|
- if let Some(gtk_win) = app.active_window() {
|
|
|
- if let Some(win) = gtk_win.downcast_ref::<ApplicationWindow>() {
|
|
|
- load_uri_into_overlay_webview(win, &url);
|
|
|
- }
|
|
|
- gtk_win.present();
|
|
|
- return glib::ExitCode::SUCCESS;
|
|
|
- }
|
|
|
-
|
|
|
- let window = ApplicationWindow::builder()
|
|
|
- .application(app)
|
|
|
- .title("Appiphany")
|
|
|
- .default_width(1024)
|
|
|
- .default_height(768)
|
|
|
- .build();
|
|
|
-
|
|
|
- let network_session = network_session_for_app(app);
|
|
|
- let webview = build_appiphany_webview(&window, &network_session);
|
|
|
- let shell = build_overlay_with_webview(&webview);
|
|
|
- window.set_content(Some(&shell));
|
|
|
- webview.load_uri(&url);
|
|
|
- window.present();
|
|
|
+ open_web_window(app, &url);
|
|
|
glib::ExitCode::SUCCESS
|
|
|
});
|
|
|
|