Skip to content

Commit

Permalink
refactor(editview|gxi): use GResources for glade files
Browse files Browse the repository at this point in the history
fixes #280
  • Loading branch information
Cogitri committed Jun 1, 2019
1 parent 18d911b commit 953f826
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 19 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ gxi/**
repo/**
cargo-vendor/**
_build/**
*.gresource
3 changes: 2 additions & 1 deletion src/editview/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ keywords = ["gxi", "xi"]
categories = ["GUI", "text-editors"]
description = "The EditView GTK widget, backed by Xi"
readme = "README.md"
build = "build.rs"

[dependencies]
cairo-rs = { git="https://github.com/gtk-rs/cairo" }
Expand All @@ -33,4 +34,4 @@ pangocairo = { git="https://github.com/gtk-rs/pangocairo" }
serde = "1"
serde_derive = "1"
serde_json = "1"
syntect = "3"
syntect = "3"
10 changes: 10 additions & 0 deletions src/editview/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use std::process::Command;

fn main() {
// Compile Gresource
Command::new("glib-compile-resources")
.args(&["--generate", "resources.xml"])
.current_dir("src/ui")
.status()
.unwrap();
}
8 changes: 8 additions & 0 deletions src/editview/src/ui/resources.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<gresources>
<gresource prefix="/com/github/Cogitri/editview/">
<file preprocess="xml-stripblanks">close_tab.glade</file>
<file preprocess="xml-stripblanks">ev.glade</file>
<file preprocess="xml-stripblanks">find_replace.glade</file>
</gresource>
</gresources>
17 changes: 10 additions & 7 deletions src/editview/src/view_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ use crate::edit_view::EditView;
use crate::main_state::MainState;
use gdk::{Cursor, CursorType, DisplayManager, WindowExt};
use gettextrs::gettext;
use gio::Resource;
use glib::Bytes;
use gtk::*;
use log::{debug, trace};
use std::cell::RefCell;
use std::rc::Rc;

const TAB_GLADE_SRC: &str = include_str!("ui/close_tab.glade");
const EV_GLADE_SRC: &str = include_str!("ui/ev.glade");
const RESOURCE: &[u8] = include_bytes!("ui/resources.gresource");

#[derive(Clone)]
pub struct EvBar {
Expand Down Expand Up @@ -36,7 +37,11 @@ pub struct ViewItem {
impl ViewItem {
/// Sets up the drawing areas and scrollbars.
pub fn new(main_state: &MainState) -> Self {
let builder = Builder::new_from_string(EV_GLADE_SRC);
let gbytes = Bytes::from_static(RESOURCE);
let resource = Resource::new_from_data(&gbytes).unwrap();
gio::resources_register(&resource);

let builder = Builder::new_from_resource("/com/github/Cogitri/editview/ev.glade");

let hadj = builder.get_object("hadj").unwrap();
let vadj = builder.get_object("vadj").unwrap();
Expand Down Expand Up @@ -184,7 +189,7 @@ pub struct TopBar {
impl TopBar {
/// Make the widgets for the tab
pub fn new() -> Self {
let builder = Builder::new_from_string(TAB_GLADE_SRC);
let builder = Builder::new_from_resource("/com/github/Cogitri/editview/close_tab.glade");
let tab_widget: Box = builder.get_object("tab_widget").unwrap();
let label = builder.get_object("tab_label").unwrap();
let close_button = builder.get_object("close_button").unwrap();
Expand Down Expand Up @@ -228,9 +233,7 @@ pub struct FindReplace {
impl FindReplace {
/// Loads the glade description of the window, and builds gtk-rs objects.
pub fn new(btn: &MenuButton) -> Self {
const SRC: &str = include_str!("ui/find_replace.glade");

let builder = Builder::new_from_string(SRC);
let builder = Builder::new_from_resource("/com/github/Cogitri/editview/find_replace.glade");
let search_bar = builder.get_object("search_bar").unwrap();
let popover: Popover = builder.get_object("search_popover").unwrap();
let replace_revealer: Revealer = builder.get_object("replace_revealer").unwrap();
Expand Down
3 changes: 2 additions & 1 deletion src/gxi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ keywords = ["gxi", "xi"]
categories = ["editor"]
license = "MIT"
edition = "2018"
build = "build.rs"

[features]
default = []
Expand Down Expand Up @@ -44,4 +45,4 @@ serde_json = "1"
syntect = "3"

[dev-dependencies]
cargo-husky = { version="1", default-features = false, features = ["user-hooks"] }
cargo-husky = { version="1", default-features = false, features = ["user-hooks"] }
10 changes: 10 additions & 0 deletions src/gxi/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use std::process::Command;

fn main() {
// Compile Gresource
Command::new("glib-compile-resources")
.args(&["--generate", "resources.xml"])
.current_dir("src/ui")
.status()
.unwrap();
}
15 changes: 9 additions & 6 deletions src/gxi/src/main_win.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use crate::errors::ErrorDialog;
use crate::prefs_win::PrefsWin;
use editview::{theme::u32_from_color, theme::LineStyle, EditView, MainState, Settings};
use gettextrs::gettext;
use gio::{ActionMapExt, ApplicationExt, SettingsExt, SimpleAction};
use glib::MainContext;
use gio::{ActionMapExt, ApplicationExt, Resource, SettingsExt, SimpleAction};
use glib::{Bytes, MainContext};
use gtk::*;
use gxi_config_storage::{GSchema, GSchemaExt};
use gxi_peer::ErrorMsg;
Expand All @@ -18,6 +18,8 @@ use std::rc::Rc;
use std::thread;
use syntect::highlighting::ThemeSettings;

pub(crate) const RESOURCE: &[u8] = include_bytes!("ui/resources.gresource");

/// Returned by an `ask_save_dialog` when we ask the user if he wants to either:
/// - `Save`(save unsaved changes and close view)
/// - `CloseWithoutSave` (discard pending changes and close view)
Expand Down Expand Up @@ -85,12 +87,13 @@ pub struct MainWin {
properties: RefCell<WinProp>,
}

const GLADE_SRC: &str = include_str!("ui/gxi.glade");

impl MainWin {
pub fn new(application: &Application, shared_queue: SharedQueue, core: Core) -> Rc<Self> {
let glade_src = GLADE_SRC;
let builder = Builder::new_from_string(glade_src);
let gbytes = Bytes::from_static(RESOURCE);
let resource = Resource::new_from_data(&gbytes).unwrap();
gio::resources_register(&resource);

let builder = Builder::new_from_resource("/com/github/Cogitri/gxi/gxi.glade");

let properties = RefCell::new(WinProp::new(&application));
let window: ApplicationWindow = builder.get_object("appwindow").unwrap();
Expand Down
11 changes: 7 additions & 4 deletions src/gxi/src/prefs_win.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use editview::MainState;
use gettextrs::gettext;
use gio::{SettingsBindFlags, SettingsExt};
use gio::{Resource, SettingsBindFlags, SettingsExt};
use glib::Bytes;
use gtk::*;
use gxi_config_storage::{GSchema, GSchemaExt};
use gxi_peer::Core;
Expand All @@ -14,16 +15,18 @@ pub struct PrefsWin {
pub window: Window,
}

const SRC: &str = include_str!("ui/prefs_win.glade");

impl PrefsWin {
pub fn new(
parent: &ApplicationWindow,
main_state: &Rc<RefCell<MainState>>,
core: &Core,
gschema: &GSchema,
) -> Self {
let builder = Builder::new_from_string(SRC);
let gbytes = Bytes::from_static(crate::main_win::RESOURCE);
let resource = Resource::new_from_data(&gbytes).unwrap();
gio::resources_register(&resource);

let builder = Builder::new_from_resource("/com/github/Cogitri/gxi/prefs_win.glade");

let window: Window = builder.get_object("prefs_win").unwrap();
let font_chooser_widget: FontChooserWidget =
Expand Down
7 changes: 7 additions & 0 deletions src/gxi/src/ui/resources.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<gresources>
<gresource prefix="/com/github/Cogitri/gxi/">
<file preprocess="xml-stripblanks">gxi.glade</file>
<file preprocess="xml-stripblanks">prefs_win.glade</file>
</gresource>
</gresources>

0 comments on commit 953f826

Please sign in to comment.