Use GtkApplication
This commit is contained in:
parent
3171a5d397
commit
15bb4e5e2d
1
Cargo.lock
generated
1
Cargo.lock
generated
@ -5,6 +5,7 @@ dependencies = [
|
|||||||
"cairo-rs 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
"cairo-rs 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"gdk 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
"gdk 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"gdk-sys 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)",
|
"gdk-sys 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
"gio 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"glib 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
"glib 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"glib-sys 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)",
|
"glib-sys 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"gtk 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
"gtk 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
@ -14,6 +14,7 @@ gdk = "0.5"
|
|||||||
gdk-sys = "0.3"
|
gdk-sys = "0.3"
|
||||||
#neovim-lib = "^0.1.1"
|
#neovim-lib = "^0.1.1"
|
||||||
phf = "0.7"
|
phf = "0.7"
|
||||||
|
gio = "0.1"
|
||||||
|
|
||||||
[dependencies.neovim-lib]
|
[dependencies.neovim-lib]
|
||||||
git = "https://github.com/daa84/neovim-lib"
|
git = "https://github.com/daa84/neovim-lib"
|
||||||
|
23
src/main.rs
23
src/main.rs
@ -1,8 +1,8 @@
|
|||||||
extern crate gtk;
|
extern crate gtk;
|
||||||
|
extern crate gio;
|
||||||
extern crate gdk;
|
extern crate gdk;
|
||||||
extern crate gdk_sys;
|
extern crate gdk_sys;
|
||||||
extern crate glib;
|
extern crate glib;
|
||||||
//extern crate glib_sys;
|
|
||||||
extern crate cairo;
|
extern crate cairo;
|
||||||
extern crate pango;
|
extern crate pango;
|
||||||
extern crate pangocairo;
|
extern crate pangocairo;
|
||||||
@ -15,19 +15,28 @@ mod ui;
|
|||||||
mod input;
|
mod input;
|
||||||
|
|
||||||
use std::thread;
|
use std::thread;
|
||||||
|
use std::env;
|
||||||
|
use gio::ApplicationExt;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
gtk::init().expect("Failed to initialize GTK");
|
let app = gtk::Application::new(Some("org.gtk.neovim-gtk"), gio::ApplicationFlags::empty()).expect("Failed to initialize GTK application");
|
||||||
|
|
||||||
|
app.connect_activate(activate);
|
||||||
|
|
||||||
|
let args: Vec<String> = env::args().collect();
|
||||||
|
let argv: Vec<&str> = args.iter().map(String::as_str).collect();
|
||||||
|
app.run(argv.len() as i32, &argv);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn activate(app: >k::Application) {
|
||||||
ui::UI.with(|ui_cell| {
|
ui::UI.with(|ui_cell| {
|
||||||
let mut ui = ui_cell.borrow_mut();
|
let mut ui = ui_cell.borrow_mut();
|
||||||
ui.init();
|
ui.init(app);
|
||||||
|
|
||||||
nvim::initialize(&mut *ui).expect("Can't start nvim instance");
|
nvim::initialize(&mut *ui).expect("Can't start nvim instance");
|
||||||
|
|
||||||
guard_dispatch_thread(&mut *ui);
|
guard_dispatch_thread(&mut *ui);
|
||||||
});
|
});
|
||||||
|
|
||||||
gtk::main();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn guard_dispatch_thread(ui: &mut ui::Ui) {
|
fn guard_dispatch_thread(ui: &mut ui::Ui) {
|
||||||
@ -35,7 +44,9 @@ fn guard_dispatch_thread(ui: &mut ui::Ui) {
|
|||||||
thread::spawn(move || {
|
thread::spawn(move || {
|
||||||
guard.join().expect("Can't join dispatch thread");
|
guard.join().expect("Can't join dispatch thread");
|
||||||
glib::idle_add(move || {
|
glib::idle_add(move || {
|
||||||
gtk::main_quit();
|
ui::UI.with(|ui_cell| {
|
||||||
|
ui_cell.borrow().destroy();
|
||||||
|
});
|
||||||
glib::Continue(false)
|
glib::Continue(false)
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
35
src/ui.rs
35
src/ui.rs
@ -9,7 +9,7 @@ use pango;
|
|||||||
use pango::FontDescription;
|
use pango::FontDescription;
|
||||||
use gtk;
|
use gtk;
|
||||||
use gtk::prelude::*;
|
use gtk::prelude::*;
|
||||||
use gtk::{Window, WindowType, DrawingArea, Grid, ToolButton, Image, Toolbar, IconSize};
|
use gtk::{ApplicationWindow, DrawingArea, Grid, ToolButton, Image, Toolbar, IconSize};
|
||||||
use gdk::{ModifierType, Event, EventKey, EventConfigure, EventButton, EventMotion, EventType};
|
use gdk::{ModifierType, Event, EventKey, EventConfigure, EventButton, EventMotion, EventType};
|
||||||
use gdk_sys;
|
use gdk_sys;
|
||||||
use glib;
|
use glib;
|
||||||
@ -45,7 +45,7 @@ pub struct Ui {
|
|||||||
pub model: UiModel,
|
pub model: UiModel,
|
||||||
nvim: Option<Neovim>,
|
nvim: Option<Neovim>,
|
||||||
drawing_area: DrawingArea,
|
drawing_area: DrawingArea,
|
||||||
window: Window,
|
window: Option<ApplicationWindow>,
|
||||||
cur_attrs: Option<Attrs>,
|
cur_attrs: Option<Attrs>,
|
||||||
bg_color: Color,
|
bg_color: Color,
|
||||||
fg_color: Color,
|
fg_color: Color,
|
||||||
@ -62,7 +62,7 @@ impl Ui {
|
|||||||
Ui {
|
Ui {
|
||||||
model: UiModel::empty(),
|
model: UiModel::empty(),
|
||||||
drawing_area: DrawingArea::new(),
|
drawing_area: DrawingArea::new(),
|
||||||
window: Window::new(WindowType::Toplevel),
|
window: None,
|
||||||
nvim: None,
|
nvim: None,
|
||||||
cur_attrs: None,
|
cur_attrs: None,
|
||||||
bg_color: COLOR_BLACK,
|
bg_color: COLOR_BLACK,
|
||||||
@ -84,7 +84,11 @@ impl Ui {
|
|||||||
self.nvim.as_mut().unwrap()
|
self.nvim.as_mut().unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn init(&mut self) {
|
pub fn destroy(&self) {
|
||||||
|
self.window.as_ref().unwrap().destroy();
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn init(&mut self, app: >k::Application) {
|
||||||
let grid = Grid::new();
|
let grid = Grid::new();
|
||||||
|
|
||||||
let button_bar = Toolbar::new();
|
let button_bar = Toolbar::new();
|
||||||
@ -121,10 +125,13 @@ impl Ui {
|
|||||||
self.drawing_area.connect_motion_notify_event(gtk_motion_notify);
|
self.drawing_area.connect_motion_notify_event(gtk_motion_notify);
|
||||||
self.drawing_area.connect_draw(gtk_draw);
|
self.drawing_area.connect_draw(gtk_draw);
|
||||||
|
|
||||||
self.window.add(&grid);
|
self.window = Some(ApplicationWindow::new(app));
|
||||||
self.window.show_all();
|
let window = self.window.as_ref().unwrap();
|
||||||
self.window.connect_key_press_event(gtk_key_press);
|
|
||||||
self.window.connect_delete_event(gtk_delete);
|
window.add(&grid);
|
||||||
|
window.show_all();
|
||||||
|
window.connect_key_press_event(gtk_key_press);
|
||||||
|
window.connect_delete_event(gtk_delete);
|
||||||
self.drawing_area.connect_configure_event(gtk_configure_event);
|
self.drawing_area.connect_configure_event(gtk_configure_event);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -180,19 +187,20 @@ fn gtk_motion_notify(_: &DrawingArea, ev: &EventMotion) -> Inhibit {
|
|||||||
fn quit() {
|
fn quit() {
|
||||||
UI.with(|ui_cell| {
|
UI.with(|ui_cell| {
|
||||||
let mut ui = ui_cell.borrow_mut();
|
let mut ui = ui_cell.borrow_mut();
|
||||||
|
ui.destroy();
|
||||||
|
|
||||||
let nvim = ui.nvim();
|
let nvim = ui.nvim();
|
||||||
nvim.ui_detach().expect("Error in ui_detach");
|
nvim.ui_detach().expect("Error in ui_detach");
|
||||||
//nvim.quit_no_save().expect("Can't stop nvim instance");
|
//nvim.quit_no_save().expect("Can't stop nvim instance");
|
||||||
});
|
});
|
||||||
gtk::main_quit();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn gtk_delete(_: &Window, _: &Event) -> Inhibit {
|
fn gtk_delete(_: &ApplicationWindow, _: &Event) -> Inhibit {
|
||||||
quit();
|
quit();
|
||||||
Inhibit(false)
|
Inhibit(false)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn gtk_key_press(_: &Window, ev: &EventKey) -> Inhibit {
|
fn gtk_key_press(_: &ApplicationWindow, ev: &EventKey) -> Inhibit {
|
||||||
if let Some(input) = convert_key(ev) {
|
if let Some(input) = convert_key(ev) {
|
||||||
UI.with(|ui_cell| {
|
UI.with(|ui_cell| {
|
||||||
let mut ui = ui_cell.borrow_mut();
|
let mut ui = ui_cell.borrow_mut();
|
||||||
@ -351,10 +359,11 @@ fn request_width(ui: &Ui) {
|
|||||||
let request_width = (ui.model.columns as f64 * ui.char_width.unwrap()) as i32;
|
let request_width = (ui.model.columns as f64 * ui.char_width.unwrap()) as i32;
|
||||||
|
|
||||||
if width != request_width || height != request_height {
|
if width != request_width || height != request_height {
|
||||||
let (win_width, win_height) = ui.window.get_size();
|
let window = ui.window.as_ref().unwrap();
|
||||||
|
let (win_width, win_height) = window.get_size();
|
||||||
let h_border = win_width - width;
|
let h_border = win_width - width;
|
||||||
let v_border = win_height - height;
|
let v_border = win_height - height;
|
||||||
ui.window.resize(request_width + h_border, request_height + v_border);
|
window.resize(request_width + h_border, request_height + v_border);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user