Skip to content

Commit

Permalink
Implement all variants
Browse files Browse the repository at this point in the history
  • Loading branch information
Ngo Iok Ui committed May 6, 2021
1 parent 01fe7d0 commit 34c5833
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 22 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ features = [
cairo-rs = "0.9"
gio = "0.9"
glib = "0.10"
gtk = "0.9"
gtk = { version = "0.9", features = ["v3_16"] }
gdk = "0.13"
gdk-pixbuf = "0.9"
image = "0.23"

sourceview = "0.9"
6 changes: 6 additions & 0 deletions src/menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ pub enum MenuItem {
Hide,

/// A standard "Services" menu item.
/// TODO Move to platform trait
///
/// ## Platform-specific
///
Expand All @@ -57,6 +58,7 @@ pub enum MenuItem {
Services,

/// A "hide all other windows" menu item.
/// TODO Move to platform trait
///
/// ## Platform-specific
///
Expand All @@ -65,6 +67,7 @@ pub enum MenuItem {
HideOthers,

/// A menu item to show all the windows for this app.
/// TODO Move to platform trait
///
/// ## Platform-specific
///
Expand Down Expand Up @@ -106,6 +109,7 @@ pub enum MenuItem {

/// An "undo" menu item; particularly useful for supporting the cut/copy/paste/undo lifecycle
/// of events.
/// TODO Move to platform trait
///
/// ## Platform-specific
///
Expand All @@ -115,6 +119,7 @@ pub enum MenuItem {

/// An "redo" menu item; particularly useful for supporting the cut/copy/paste/undo lifecycle
/// of events.
/// TODO Move to platform trait
///
/// ## Platform-specific
///
Expand Down Expand Up @@ -155,6 +160,7 @@ pub enum MenuItem {
Minimize,

/// An item for instructing the app to zoom
/// TODO Move to platform trait
///
/// ## Platform-specific
///
Expand Down
71 changes: 68 additions & 3 deletions src/platform_impl/linux/event_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ use std::{

use gdk::{Cursor, CursorType, WindowExt, WindowState};
use gio::{prelude::*, Cancellable};
use glib::{source::idle_add_local, Continue, MainContext};
use gtk::{prelude::*, AboutDialog, ApplicationWindow, Inhibit};
use glib::{source::idle_add_local, Cast, Continue, MainContext};
use gtk::{prelude::*, AboutDialog, ApplicationWindow, Clipboard, Entry, Inhibit};

use crate::{
dpi::{PhysicalPosition, PhysicalSize},
Expand Down Expand Up @@ -483,8 +483,73 @@ impl<T: 'static> EventLoop<T> {
about.show_all();
window_target.p.app.add_window(&about);
}
MenuItem::Hide => window.hide(),
MenuItem::CloseWindow => window.close(),
_ => window.close(),
MenuItem::Quit => {
keep_running_.replace(false);
}
MenuItem::Cut => {
if let Some(widget) = window.get_focus() {
if widget.has_focus() {
if let Some(view) = widget.dynamic_cast_ref::<sourceview::View>() {
if let Some(clipboard) = Clipboard::get_default(&widget.get_display()) {
if let Some(buf) = view.get_buffer() {
buf.cut_clipboard(&clipboard, true);
}
}
} else if let Some(entry) = widget.dynamic_cast_ref::<Entry>() {
entry.cut_clipboard();
}
}
}
}
MenuItem::Copy => {
if let Some(widget) = window.get_focus() {
if widget.has_focus() {
if let Some(view) = widget.dynamic_cast_ref::<sourceview::View>() {
if let Some(clipboard) = Clipboard::get_default(&widget.get_display()) {
if let Some(buf) = view.get_buffer() {
buf.copy_clipboard(&clipboard);
}
}
} else if let Some(entry) = widget.dynamic_cast_ref::<Entry>() {
entry.copy_clipboard();
}
}
}
}
MenuItem::Paste => {
if let Some(widget) = window.get_focus() {
if widget.has_focus() {
if let Some(view) = widget.dynamic_cast_ref::<sourceview::View>() {
if let Some(clipboard) = Clipboard::get_default(&widget.get_display()) {
if let Some(buf) = view.get_buffer() {
buf.paste_clipboard(&clipboard, None, true);
}
}
} else if let Some(entry) = widget.dynamic_cast_ref::<Entry>() {
entry.paste_clipboard();
}
}
}
}
MenuItem::SelectAll => {
if let Some(widget) = window.get_focus() {
if widget.has_focus() {
if let Some(view) = widget.dynamic_cast_ref::<sourceview::View>() {
if let Some(buf) = view.get_buffer() {
buf.select_range(&buf.get_start_iter(), &buf.get_end_iter());
}
} else if let Some(entry) = widget.dynamic_cast_ref::<Entry>() {
entry.select_region(0, -1);
}
}
}
}
// TODO toggle fullscreen
MenuItem::EnterFullScreen => window.fullscreen(),
MenuItem::Minimize => window.iconify(),
_ => {}
}
}
}
Expand Down
36 changes: 19 additions & 17 deletions src/platform_impl/linux/menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::sync::mpsc::Sender;

use gtk::{
prelude::*, AccelFlags, AccelGroup, ApplicationWindow, Menu, MenuBar, MenuItem, Orientation,
SeparatorMenuItem,
};

use super::window::{WindowId, WindowRequest};
Expand All @@ -12,7 +13,7 @@ macro_rules! menuitem {
let item = MenuItem::with_label($description);
let (key, mods) = gtk::accelerator_parse($key);
item.add_accelerator("activate", &$accel_group, key, mods, AccelFlags::VISIBLE);
item
Some(item)
}};
}

Expand All @@ -35,35 +36,36 @@ pub fn initialize(
let item = match &i {
RootMenuItem::Custom(m) => match m.keyboard_accelerators {
Some(accel) => menuitem!(&m.name, accel, accel_group),
None => MenuItem::with_label(&m.name),
None => Some(MenuItem::with_label(&m.name)),
},
RootMenuItem::About(s) => MenuItem::with_label(&format!("About {}", s)),
RootMenuItem::About(s) => Some(MenuItem::with_label(&format!("About {}", s))),
RootMenuItem::Hide => menuitem!("Hide", "<Ctrl>H", accel_group),
RootMenuItem::Services => MenuItem::with_label("Services"),
RootMenuItem::HideOthers => menuitem!("Hide Others", "<Ctrl><Alt>H", accel_group),
RootMenuItem::ShowAll => MenuItem::with_label("Show All"),
RootMenuItem::CloseWindow => menuitem!("Close Window", "<Ctrl>W", accel_group),
RootMenuItem::Quit => menuitem!("Quit", "Q", accel_group),
RootMenuItem::Copy => menuitem!("Copy", "<Ctrl>C", accel_group),
RootMenuItem::Cut => menuitem!("Cut", "<Ctrl>X", accel_group),
RootMenuItem::Undo => menuitem!("Undo", "<Ctrl>Z", accel_group),
RootMenuItem::Redo => menuitem!("Redo", "<Ctrl><Shift>Z", accel_group),
RootMenuItem::SelectAll => menuitem!("Select All", "<Ctrl>A", accel_group),
RootMenuItem::Paste => menuitem!("Paste", "<Ctrl>V", accel_group),
RootMenuItem::Zoom => MenuItem::with_label("Zoom"),
RootMenuItem::Separator => MenuItem::with_label("Separator"),
RootMenuItem::Separator => {
let item = SeparatorMenuItem::new();
submenu.append(&item);
None
}
RootMenuItem::EnterFullScreen => menuitem!("Fullscreen", "F11", accel_group),
RootMenuItem::Minimize => menuitem!("Minimize", "<Ctrl>M", accel_group),
_ => None,
};

let tx_ = tx.clone();
item.connect_activate(move |_| {
if let Err(e) = tx_.send((id, WindowRequest::Menu(i.clone()))) {
log::warn!("Fail to send menu request: {}", e);
}
});
if let Some(item) = item {
let tx_ = tx.clone();
item.connect_activate(move |_| {
if let Err(e) = tx_.send((id, WindowRequest::Menu(i.clone()))) {
log::warn!("Fail to send menu request: {}", e);
}
});

submenu.append(&item);
submenu.append(&item);
}
}

title.set_submenu(Some(&submenu));
Expand Down

0 comments on commit 34c5833

Please sign in to comment.