Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Port to libadwaita #148

Merged
merged 7 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ You'll need the following dependencies:

* blueprint-compiler
* [libchcase](https://github.com/ryonakano/chcase)
* libgranite-7-dev
* libadwaita-1-dev (>= 1.4)
* libgranite-7-dev (>= 7.2.0, required only when you build with `granite` feature enabled)
* libgtk4-dev
* libgtksourceview-5-dev
* meson (>= 0.57.0)
Expand Down
1 change: 1 addition & 0 deletions com.github.ryonakano.konbucase.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ modules:
buildsystem: meson
config-opts:
- '-Duse_submodule=false'
- '-Dgranite=enabled'
sources:
- type: dir
path: .
74 changes: 35 additions & 39 deletions data/resources/ui/main-window.blp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Gtk 4.0;
using Granite 7.0;
using Adw 1;

// Main menu model
menu main_menu {
Expand Down Expand Up @@ -42,53 +42,49 @@ $MainWindowModel window_model {
/**
* MainWindow: The app window
*/
template $MainWindow : Gtk.ApplicationWindow {
template $MainWindow : Adw.ApplicationWindow {
width-request: 700;
height-request: 500;
title: "KonbuCase";

[titlebar]
Gtk.HeaderBar {
[end]
Gtk.MenuButton {
tooltip-text: _("Main Menu");
icon-name: "open-menu";
menu-model: main_menu;
primary: true;
Adw.ToolbarView {
[top]
Adw.HeaderBar {
[end]
Gtk.MenuButton {
tooltip-text: _("Main Menu");
icon-name: "open-menu";
menu-model: main_menu;
primary: true;
}
}
}

// Use overlay for the toast
Gtk.Overlay {
[overlay]
Gtk.Box {
orientation: horizontal;
spacing: 0;
// Use overlay for the toast
content: Adw.ToastOverlay overlay {
Gtk.Box {
orientation: horizontal;
spacing: 0;

// Left pane for input text
$TextPane source_pane {
model: source_model;
header-label: _("Convert from:");
editable: true;
}
// Left pane for input text
$TextPane source_pane {
model: source_model;
header-label: _("Convert from:");
editable: true;
}

Gtk.Separator {
orientation: vertical;
vexpand: true;
}
Gtk.Separator {
orientation: vertical;
vexpand: true;
}

// Right pane for output text
$TextPane result_pane {
model: result_model;
header-label: _("Convert to:");
// Make the text view uneditable, otherwise the app freezes
editable: false;
// Right pane for output text
$TextPane result_pane {
model: result_model;
header-label: _("Convert to:");
// Make the text view uneditable, otherwise the app freezes
editable: false;
}
}
}

[overlay]
Granite.Toast toast {
title: _("Text copied!");
}
};
}
}
1 change: 1 addition & 0 deletions meson_options.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
option('use_submodule', type: 'boolean', value: true, description: 'Whether to build using git submodule')
option('granite', type: 'feature', value: 'disabled', description: 'Enable elementary OS integration. Requires Granite')
25 changes: 19 additions & 6 deletions src/Application.vala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* SPDX-FileCopyrightText: 2020-2024 Ryo Nakano <ryonakaknock3@gmail.com>
*/

public class Application : Gtk.Application {
public class Application : Adw.Application {
public static bool IS_ON_PANTHEON {
get {
return Environment.get_variable ("XDG_CURRENT_DESKTOP") == "Pantheon";
Expand All @@ -16,7 +16,6 @@ public class Application : Gtk.Application {
{ "quit", on_quit_activate },
};
private MainWindow window;
private StyleManager style_manager;

public Application () {
Object (
Expand All @@ -30,20 +29,34 @@ public class Application : Gtk.Application {
}

private void setup_style () {
style_manager = StyleManager.get_default ();

var style_action = new SimpleAction.stateful (
"color-scheme", VariantType.STRING, new Variant.string (StyleManager.COLOR_SCHEME_DEFAULT)
"color-scheme", VariantType.STRING, new Variant.string (Define.ColorScheme.DEFAULT)
);
style_action.bind_property ("state", style_manager, "color-scheme",
BindingFlags.BIDIRECTIONAL | BindingFlags.SYNC_CREATE,
Util.style_action_transform_to_cb,
Util.style_action_transform_from_cb);
settings.bind ("color-scheme", style_manager, "color-scheme", SettingsBindFlags.DEFAULT);
settings.bind_with_mapping ("color-scheme", style_manager, "color-scheme", SettingsBindFlags.DEFAULT,
Util.color_scheme_get_mapping_cb,
Util.color_scheme_set_mapping_cb,
null, null);
add_action (style_action);
}

protected override void startup () {
#if USE_GRANITE
/*
* Use both compile-time and runtime conditions to:
*
* * make Granite optional dependency
* * make sure to respect currently running DE
*/
if (IS_ON_PANTHEON) {
// Apply elementary stylesheet instead of default Adwaita stylesheet
Granite.init ();
}
#endif

base.startup ();

Intl.setlocale (LocaleCategory.ALL, "");
Expand Down
14 changes: 14 additions & 0 deletions src/Define.vala
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,20 @@
*/

namespace Define {
/**
* String representation of Adw.ColorScheme.
*
* Note: Only defines necessary strings for the app.
*/
namespace ColorScheme {
/** Inherit the parent color-scheme. */
public const string DEFAULT = "default";
/** Always use light appearance. */
public const string FORCE_LIGHT = "force-light";
/** Always use dark appearance. */
public const string FORCE_DARK = "force-dark";
}

/**
* Type of the text.
*/
Expand Down
56 changes: 0 additions & 56 deletions src/StyleManager.vala

This file was deleted.

111 changes: 101 additions & 10 deletions src/Util.vala
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ namespace Util {
}
}

/**
* Convert ``from_value`` to ``to_value``.
*
* @param binding a binding
* @param from_value the value of Action.state property
* @param to_value the value of Adw.StyleManager.color_scheme property
*
* @return true if the transformation was successful, false otherwise
* @see GLib.BindingTransformFunc
*/
public static bool style_action_transform_to_cb (Binding binding, Value from_value, ref Value to_value) {
Variant? variant = from_value.dup_variant ();
if (variant == null) {
Expand All @@ -43,10 +53,14 @@ namespace Util {

var val = variant.get_string ();
switch (val) {
case StyleManager.COLOR_SCHEME_DEFAULT:
case StyleManager.COLOR_SCHEME_FORCE_LIGHT:
case StyleManager.COLOR_SCHEME_FORCE_DARK:
to_value.set_string (val);
case Define.ColorScheme.DEFAULT:
to_value.set_enum (Adw.ColorScheme.DEFAULT);
break;
case Define.ColorScheme.FORCE_LIGHT:
to_value.set_enum (Adw.ColorScheme.FORCE_LIGHT);
break;
case Define.ColorScheme.FORCE_DARK:
to_value.set_enum (Adw.ColorScheme.FORCE_DARK);
break;
default:
warning ("style_action_transform_to_cb: Invalid color scheme: %s", val);
Expand All @@ -56,20 +70,97 @@ namespace Util {
return true;
}

/**
* Convert ``from_value`` to ``to_value``.
*
* @param binding a binding
* @param from_value the value of Adw.StyleManager.color_scheme property
* @param to_value the value of Action.state property
*
* @return true if the transformation was successful, false otherwise
* @see GLib.BindingTransformFunc
*/
public static bool style_action_transform_from_cb (Binding binding, Value from_value, ref Value to_value) {
var val = (string) from_value;
var val = (Adw.ColorScheme) from_value;
switch (val) {
case Adw.ColorScheme.DEFAULT:
to_value.set_variant (new Variant.string (Define.ColorScheme.DEFAULT));
break;
case Adw.ColorScheme.FORCE_LIGHT:
to_value.set_variant (new Variant.string (Define.ColorScheme.FORCE_LIGHT));
break;
case Adw.ColorScheme.FORCE_DARK:
to_value.set_variant (new Variant.string (Define.ColorScheme.FORCE_DARK));
break;
default:
warning ("style_action_transform_from_cb: Invalid color scheme: %d", val);
return false;
}

return true;
}

/**
* Convert from the "style" enum defined in the gschema to Adw.ColorScheme.
*
* @param value the Value containing Adw.ColorScheme value
* @param variant the Variant containing "style" enum value defined in the gschema
* @param user_data unused (null)
*
* @return true if the conversion succeeded, false otherwise
* @see GLib.SettingsBindGetMappingShared
*/
public static bool color_scheme_get_mapping_cb (Value value, Variant variant, void* user_data) {
var val = variant.get_string ();
switch (val) {
case StyleManager.COLOR_SCHEME_DEFAULT:
case StyleManager.COLOR_SCHEME_FORCE_LIGHT:
case StyleManager.COLOR_SCHEME_FORCE_DARK:
to_value.set_variant (new Variant.string (val));
case Define.ColorScheme.DEFAULT:
value.set_enum (Adw.ColorScheme.DEFAULT);
break;
case Define.ColorScheme.FORCE_LIGHT:
value.set_enum (Adw.ColorScheme.FORCE_LIGHT);
break;
case Define.ColorScheme.FORCE_DARK:
value.set_enum (Adw.ColorScheme.FORCE_DARK);
break;
default:
warning ("style_action_transform_from_cb: Invalid color scheme: %s", val);
warning ("color_scheme_get_mapping_cb: Invalid style: %s", val);
return false;
}

return true;
}

/**
* Convert from Adw.ColorScheme to the "style" enum defined in the gschema.
*
* @param value the Value containing Adw.ColorScheme value
* @param expected_type the expected type of Variant that this method returns
* @param user_data unused (null)
*
* @return a new Variant containing "style" enum value defined in the gschema
* @see GLib.SettingsBindSetMappingShared
*/
public static Variant color_scheme_set_mapping_cb (Value value, VariantType expected_type, void* user_data) {
string color_scheme;

var val = (Adw.ColorScheme) value;
switch (val) {
case Adw.ColorScheme.DEFAULT:
color_scheme = Define.ColorScheme.DEFAULT;
break;
case Adw.ColorScheme.FORCE_LIGHT:
color_scheme = Define.ColorScheme.FORCE_LIGHT;
break;
case Adw.ColorScheme.FORCE_DARK:
color_scheme = Define.ColorScheme.FORCE_DARK;
break;
default:
warning ("color_scheme_set_mapping_cb: Invalid Adw.ColorScheme: %d", val);
// fallback to default
color_scheme = Define.ColorScheme.DEFAULT;
break;
}

return new Variant.string (color_scheme);
}
}
Loading