Intial GTK, swtich to Relm, basic Flatpak Build

This commit is contained in:
dav 2023-07-01 12:48:55 -05:00 committed by Conrad Kramer
parent f1d7a98491
commit 60257b256a
29 changed files with 2573 additions and 563 deletions

View file

@ -1,92 +0,0 @@
use gtk::prelude::*;
use adw::subclass::prelude::*;
use gtk::{gio, glib};
use crate::config::VERSION;
use crate::BurrowGtkWindow;
mod imp {
use super::*;
#[derive(Debug, Default)]
pub struct BurrowGtkApplication {}
#[glib::object_subclass]
impl ObjectSubclass for BurrowGtkApplication {
const NAME: &'static str = "BurrowGtkApplication";
type Type = super::BurrowGtkApplication;
type ParentType = adw::Application;
}
impl ObjectImpl for BurrowGtkApplication {
fn constructed(&self) {
self.parent_constructed();
let obj = self.obj();
obj.setup_gactions();
obj.set_accels_for_action("app.quit", &["<primary>q"]);
}
}
impl ApplicationImpl for BurrowGtkApplication {
// We connect to the activate callback to create a window when the application
// has been launched. Additionally, this callback notifies us when the user
// tries to launch a "second instance" of the application. When they try
// to do that, we'll just present any existing window.
fn activate(&self) {
let application = self.obj();
// Get the current window or create one if necessary
let window = if let Some(window) = application.active_window() {
window
} else {
let window = BurrowGtkWindow::new(&*application);
window.upcast()
};
// Ask the window manager/compositor to present the window
window.present();
}
}
impl GtkApplicationImpl for BurrowGtkApplication {}
impl AdwApplicationImpl for BurrowGtkApplication {}
}
glib::wrapper! {
pub struct BurrowGtkApplication(ObjectSubclass<imp::BurrowGtkApplication>)
@extends gio::Application, gtk::Application, adw::Application,
@implements gio::ActionGroup, gio::ActionMap;
}
impl BurrowGtkApplication {
pub fn new(application_id: &str, flags: &gio::ApplicationFlags) -> Self {
glib::Object::builder()
.property("application-id", application_id)
.property("flags", flags)
.build()
}
fn setup_gactions(&self) {
let quit_action = gio::ActionEntry::builder("quit")
.activate(move |app: &Self, _, _| app.quit())
.build();
let about_action = gio::ActionEntry::builder("about")
.activate(move |app: &Self, _, _| app.show_about())
.build();
self.add_action_entries([quit_action, about_action]);
}
fn show_about(&self) {
let window = self.active_window().unwrap();
let about = adw::AboutWindow::builder()
.transient_for(&window)
.application_name("burrow-gtk")
.application_icon("com.hackclub.Burrow")
.developer_name("Hack Club")
.version(VERSION)
.developers(vec!["Hack Club"])
.copyright("© 2023 The Hack Foundation")
.build();
about.present();
}
}

View file

@ -1,7 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<gresources>
<gresource prefix="/com/hackclub/Burrow">
<file preprocess="xml-stripblanks">window.ui</file>
<file preprocess="xml-stripblanks">gtk/help-overlay.ui</file>
</gresource>
</gresources>

View file

@ -1,4 +0,0 @@
pub static VERSION: &str = "0.1.0";
pub static GETTEXT_PACKAGE: &str = "burrow-gtk";
pub static LOCALEDIR: &str = "/app/share/locale";
pub static PKGDATADIR: &str = "/app/share/burrow-gtk";

View file

@ -1,4 +0,0 @@
pub static VERSION: &str = @VERSION@;
pub static GETTEXT_PACKAGE: &str = @GETTEXT_PACKAGE@;
pub static LOCALEDIR: &str = @LOCALEDIR@;
pub static PKGDATADIR: &str = @PKGDATADIR@;

View file

@ -1,24 +0,0 @@
using Gtk 4.0;
ShortcutsWindow help_overlay {
modal: true;
ShortcutsSection {
section-name: "shortcuts";
max-height: 10;
ShortcutsGroup {
title: C_("shortcut window", "General");
ShortcutsShortcut {
title: C_("shortcut window", "Show Shortcuts");
action-name: "win.show-help-overlay";
}
ShortcutsShortcut {
title: C_("shortcut window", "Quit");
action-name: "app.quit";
}
}
}
}

View file

@ -1,35 +1,87 @@
mod application;
mod config;
mod window;
use adw::prelude::*;
use burrow::{DaemonClient, DaemonCommand, DaemonStartOptions};
use gtk::Align;
use relm4::{
component::{AsyncComponent, AsyncComponentParts, AsyncComponentSender},
prelude::*,
};
use self::application::BurrowGtkApplication;
use self::window::BurrowGtkWindow;
struct App {}
use config::{GETTEXT_PACKAGE, LOCALEDIR, PKGDATADIR};
use gettextrs::{bind_textdomain_codeset, bindtextdomain, textdomain};
use gtk::{gio, glib};
use gtk::prelude::*;
fn main() -> glib::ExitCode {
// Set up gettext translations
bindtextdomain(GETTEXT_PACKAGE, LOCALEDIR).expect("Unable to bind the text domain");
bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8")
.expect("Unable to set the text domain encoding");
textdomain(GETTEXT_PACKAGE).expect("Unable to switch to the text domain");
// Load resources
let resources = gio::Resource::load(PKGDATADIR.to_owned() + "/burrow-gtk.gresource")
.expect("Could not load resources");
gio::resources_register(&resources);
// Create a new GtkApplication. The application manages our main loop,
// application windows, integration with the window manager/compositor, and
// desktop features such as file opening and single-instance applications.
let app = BurrowGtkApplication::new("com.hackclub.Burrow", &gio::ApplicationFlags::empty());
// Run the application. This function will block until the application
// exits. Upon return, we have our exit code to return to the shell. (This
// is the code you see when you do `echo $?` after running a command in a
// terminal.
app.run()
#[derive(Debug)]
enum Msg {
Start,
Stop,
}
#[relm4::component(async)]
impl AsyncComponent for App {
type Init = ();
type Input = Msg;
type Output = ();
type CommandOutput = ();
view! {
adw::Window {
set_title: Some("Simple app"),
set_default_size: (640, 480),
gtk::Box {
set_orientation: gtk::Orientation::Vertical,
set_spacing: 5,
set_margin_all: 5,
set_valign: Align::Center,
gtk::Label {
set_label: "Burrow GTK Switch",
},
gtk::Switch {
set_halign: Align::Center,
set_hexpand: false,
set_vexpand: false,
connect_active_notify => move |switch|
sender.input(if switch.is_active() { Msg::Start } else { Msg::Stop })
},
}
}
}
async fn init(
_: Self::Init,
root: Self::Root,
sender: AsyncComponentSender<Self>,
) -> AsyncComponentParts<Self> {
let model = App {};
let widgets = view_output!();
AsyncComponentParts { model, widgets }
}
async fn update(
&mut self,
msg: Self::Input,
_sender: AsyncComponentSender<Self>,
_root: &Self::Root,
) {
match msg {
Msg::Start => {
let mut client = DaemonClient::new().await.unwrap();
client
.send_command(DaemonCommand::Start(DaemonStartOptions::default()))
.await
.unwrap();
}
Msg::Stop => {
let mut client = DaemonClient::new().await.unwrap();
client.send_command(DaemonCommand::Stop).await.unwrap();
}
}
}
}
fn main() {
let app = RelmApp::new("com.hackclub.burrow");
app.run_async::<App>(());
}

View file

@ -1,67 +0,0 @@
pkgdatadir = join_paths(get_option('prefix'), get_option('datadir'), meson.project_name())
gnome = import('gnome')
blueprints = custom_target('blueprints',
input: files(
'gtk/help-overlay.blp',
'window.blp',
),
output: '.',
command: [find_program('blueprint-compiler'), 'batch-compile', '@OUTPUT@', '@CURRENT_SOURCE_DIR@', '@INPUT@'],
)
gnome.compile_resources('burrow-gtk',
'burrow-gtk.gresource.xml',
gresource_bundle: true,
install: true,
install_dir: pkgdatadir,
dependencies: blueprints
)
conf = configuration_data()
conf.set_quoted('VERSION', meson.project_version())
conf.set_quoted('GETTEXT_PACKAGE', 'burrow-gtk')
conf.set_quoted('LOCALEDIR', join_paths(get_option('prefix'), get_option('localedir')))
conf.set_quoted('PKGDATADIR', pkgdatadir)
configure_file(
input: 'config.rs.in',
output: 'config.rs',
configuration: conf
)
# Copy the config.rs output to the source directory.
run_command(
'cp',
join_paths(meson.project_build_root(), 'src', 'config.rs'),
join_paths(meson.project_source_root(), 'src', 'config.rs'),
check: true
)
cargo_bin = find_program('cargo')
cargo_opt = [ '--manifest-path', meson.project_source_root() / 'Cargo.toml' ]
cargo_opt += [ '--target-dir', meson.project_build_root() / 'src' ]
cargo_env = [ 'CARGO_HOME=' + meson.project_build_root() / 'cargo-home' ]
if get_option('buildtype') == 'release'
cargo_options += [ '--release' ]
rust_target = 'release'
else
rust_target = 'debug'
endif
cargo_build = custom_target(
'cargo-build',
build_by_default: true,
build_always_stale: true,
output: meson.project_name(),
console: true,
install: true,
install_dir: get_option('bindir'),
command: [
'env', cargo_env,
cargo_bin, 'build',
cargo_opt, '&&', 'cp', 'src' / rust_target / meson.project_name(), '@OUTPUT@',
]
)

View file

@ -1,48 +0,0 @@
using Gtk 4.0;
using Adw 1;
template BurrowGtkWindow : Adw.ApplicationWindow {
default-width: 600;
default-height: 400;
Box {
orientation: vertical;
HeaderBar header_bar {
[end]
MenuButton {
icon-name: "open-menu-symbolic";
menu-model: primary_menu;
}
}
Label label {
label: "Burrow GNOME";
vexpand: true;
styles [
"title-1",
]
}
}
}
menu primary_menu {
section {
item {
label: _("_Preferences");
action: "app.preferences";
}
item {
label: _("_Keyboard Shortcuts");
action: "win.show-help-overlay";
}
item {
label: _("_About Burrow-gtk");
action: "app.about";
}
}
}

View file

@ -1,51 +0,0 @@
use gtk::prelude::*;
use adw::subclass::prelude::*;
use gtk::{gio, glib};
mod imp {
use super::*;
#[derive(Debug, Default, gtk::CompositeTemplate)]
#[template(resource = "/com/hackclub/Burrow/window.ui")]
pub struct BurrowGtkWindow {
// Template widgets
#[template_child]
pub header_bar: TemplateChild<gtk::HeaderBar>,
#[template_child]
pub label: TemplateChild<gtk::Label>,
}
#[glib::object_subclass]
impl ObjectSubclass for BurrowGtkWindow {
const NAME: &'static str = "BurrowGtkWindow";
type Type = super::BurrowGtkWindow;
type ParentType = adw::ApplicationWindow;
fn class_init(klass: &mut Self::Class) {
klass.bind_template();
}
fn instance_init(obj: &glib::subclass::InitializingObject<Self>) {
obj.init_template();
}
}
impl ObjectImpl for BurrowGtkWindow {}
impl WidgetImpl for BurrowGtkWindow {}
impl WindowImpl for BurrowGtkWindow {}
impl ApplicationWindowImpl for BurrowGtkWindow {}
impl AdwApplicationWindowImpl for BurrowGtkWindow {}
}
glib::wrapper! {
pub struct BurrowGtkWindow(ObjectSubclass<imp::BurrowGtkWindow>)
@extends gtk::Widget, gtk::Window, gtk::ApplicationWindow, adw::ApplicationWindow, @implements gio::ActionGroup, gio::ActionMap;
}
impl BurrowGtkWindow {
pub fn new<P: glib::IsA<gtk::Application>>(application: &P) -> Self {
glib::Object::builder()
.property("application", application)
.build()
}
}