Skip to content
Draft
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
24 changes: 19 additions & 5 deletions plugins/brackets-completion/brackets-completion.vala
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public class Scratch.Plugins.BracketsCompletion : Peas.ExtensionBase, Scratch.Se

public Object object { owned get; set construct; }

private Gtk.EventControllerKey key_controller;
private Gee.HashMap<string, string> brackets;
private Gee.HashMap<uint, string> keys;
private Gtk.TextBuffer current_buffer;
Expand Down Expand Up @@ -40,6 +41,12 @@ public class Scratch.Plugins.BracketsCompletion : Peas.ExtensionBase, Scratch.Se

plugins = (Scratch.Services.Interface) object;
plugins.hook_document.connect (on_hook_document);
plugins.hook_window.connect ((w) => {
key_controller = new Gtk.EventControllerKey (w) {
propagation_phase = CAPTURE
};
key_controller.key_pressed.connect (on_key_down);
});
}

public void deactivate () {
Expand All @@ -52,14 +59,12 @@ public class Scratch.Plugins.BracketsCompletion : Peas.ExtensionBase, Scratch.Se
current_buffer = doc.source_view.buffer;

if (current_source_view != null) {
current_source_view.key_press_event.disconnect (on_key_down);
current_source_view.event_after.disconnect (on_event_after);
current_source_view.backspace.disconnect (on_backspace);
}

current_source_view = doc.source_view;

current_source_view.key_press_event.connect (on_key_down);
current_source_view.event_after.connect (on_event_after);
current_source_view.backspace.connect (on_backspace);
}
Expand Down Expand Up @@ -163,8 +168,17 @@ public class Scratch.Plugins.BracketsCompletion : Peas.ExtensionBase, Scratch.Se
current_buffer.end_user_action ();
}

private bool on_key_down (Gdk.EventKey event) {
if (Gdk.ModifierType.MOD1_MASK in event.state || Gdk.ModifierType.CONTROL_MASK in event.state) {
private bool on_key_down (
uint keyval,
uint keycode,
Gdk.ModifierType state
) requires (current_source_view != null && current_buffer != null) {

if (!current_source_view.is_focus) {
return false;
}

if (Gdk.ModifierType.MOD1_MASK in state || Gdk.ModifierType.CONTROL_MASK in state) {
return false;
}

Expand All @@ -173,7 +187,7 @@ public class Scratch.Plugins.BracketsCompletion : Peas.ExtensionBase, Scratch.Se
return false;
}

if (keys.has_key (event.keyval) && current_buffer.has_selection) {
if (keys.has_key (keyval) && current_buffer.has_selection) {
Gtk.TextIter start, end;
current_buffer.get_selection_bounds (out start, out end);

Expand Down
48 changes: 32 additions & 16 deletions plugins/markdown-actions/markdown-actions.vala
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@ public class Code.Plugins.MarkdownActions : Peas.ExtensionBase, Scratch.Services

public void update_state () {}

private Gtk.EventControllerKey key_controller;
private bool is_markdown = false;

public void activate () {
plugins = (Scratch.Services.Interface) object;
plugins.hook_document.connect ((doc) => {
if (current_source != null) {
current_source.key_press_event.disconnect (shortcut_handler);
current_source.notify["language"].disconnect (configure_shortcuts);
}

Expand All @@ -39,30 +41,44 @@ public class Code.Plugins.MarkdownActions : Peas.ExtensionBase, Scratch.Services

current_source.notify["language"].connect (configure_shortcuts);
});
plugins.hook_window.connect ((w) => {
key_controller = new Gtk.EventControllerKey (w) {
propagation_phase = CAPTURE
};
key_controller.key_pressed.connect (shortcut_handler);
});
}

private void configure_shortcuts () {
var lang = current_source.language;
if (lang != null && lang.id == "markdown") {
current_source.key_press_event.connect (shortcut_handler);
} else {
current_source.key_press_event.disconnect (shortcut_handler);
}
is_markdown = (lang != null && lang.id == "markdown");
}

private bool shortcut_handler (Gdk.EventKey evt) {
var control = (evt.state & Gdk.ModifierType.CONTROL_MASK) != 0;
var shift = (evt.state & Gdk.ModifierType.SHIFT_MASK) != 0;
var other_mods = (evt.state & Gtk.accelerator_get_default_mod_mask () &
~Gdk.ModifierType.SHIFT_MASK &
~Gdk.ModifierType.CONTROL_MASK) != 0;
private bool shortcut_handler (
Gtk.EventController controller,
uint keyval,
uint keycode,
Gdk.ModifierType state
) requires (current_source != null) {

if (evt.is_modifier == 1 || other_mods == true) {
if (!current_source.is_focus) {
return false;
}

if (!is_markdown || !Gtk.accelerator_valid (keyval, state)) {
return false;
}

var mods = (state & Gtk.accelerator_get_default_mod_mask ());
if (((mods & ~Gdk.ModifierType.SHIFT_MASK) & ~Gdk.ModifierType.CONTROL_MASK) != 0) {
// A modifier other than Control or Shift is down
return false;
}

var control = (mods & Gdk.ModifierType.CONTROL_MASK) != 0;
var shift = (mods & Gdk.ModifierType.SHIFT_MASK) != 0;
if (control && shift) {
switch (evt.keyval) {
switch (keyval) {
case Gdk.Key.B:
add_markdown_tag ("**");
return true;
Expand All @@ -75,7 +91,7 @@ public class Code.Plugins.MarkdownActions : Peas.ExtensionBase, Scratch.Services
}
}

if (evt.keyval == Gdk.Key.Return) {
if (keyval == Gdk.Key.Return) {
char ul_marker;
int ol_number = 1;
string item_text;
Expand All @@ -99,6 +115,7 @@ public class Code.Plugins.MarkdownActions : Peas.ExtensionBase, Scratch.Services
return true;
}
}

return false;
}

Expand Down Expand Up @@ -233,7 +250,6 @@ public class Code.Plugins.MarkdownActions : Peas.ExtensionBase, Scratch.Services

public void deactivate () {
if (current_source != null) {
current_source.key_press_event.disconnect (shortcut_handler);
current_source.notify["language"].disconnect (configure_shortcuts);
}
}
Expand Down
46 changes: 33 additions & 13 deletions plugins/vim-emulation/vim-emulation.vala
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ public class Scratch.Plugins.VimEmulation : Peas.ExtensionBase, Scratch.Services
Scratch.Widgets.SourceView? view = null;

Scratch.Services.Interface plugins;
private Gtk.EventControllerKey key_controller;

public Object object { owned get; set construct; }

construct {
Expand All @@ -48,36 +50,49 @@ public class Scratch.Plugins.VimEmulation : Peas.ExtensionBase, Scratch.Services
plugins = (Scratch.Services.Interface) object;
plugins.hook_document.connect ((doc) => {
this.view = doc.source_view;
this.view.key_press_event.disconnect (handle_key_press);
this.view.key_press_event.connect (handle_key_press);
this.views.add (view);
});
plugins.hook_window.connect ((w) => {
key_controller = new Gtk.EventControllerKey (w) {
propagation_phase = CAPTURE
};

key_controller.key_pressed.connect (handle_key_press);
});
}

public void deactivate () {
foreach (var v in views) {
v.key_press_event.disconnect (handle_key_press);
}

}

private bool handle_key_press (Gdk.EventKey event) {
private bool handle_key_press (
Gtk.EventController controller,
uint keyval,
uint keycode,
Gdk.ModifierType state
) requires (view != null) {

if (!view.is_focus) {
return false;
}

//some extensions to the default navigating
bool ctrl = (event.state & Gdk.ModifierType.CONTROL_MASK) != 0;
bool shift = (event.state & Gdk.ModifierType.SHIFT_MASK) != 0;
bool ctrl = (state & Gdk.ModifierType.CONTROL_MASK) != 0;
bool shift = (state & Gdk.ModifierType.SHIFT_MASK) != 0;

if (ctrl && event.keyval == Gdk.Key.Up) {
if (ctrl && (keyval == Gdk.Key.Up)) {
move_paragraph (true, shift);
return true;
}

if (ctrl && event.keyval == Gdk.Key.Down) {
if (ctrl && (keyval == Gdk.Key.Down)) {
move_paragraph (false, shift);
return true;
}

int old_len = number.length;
// Firstly let's set the mode
switch (event.keyval) {
switch (keyval) {
//mode changing
case Gdk.Key.i:
if (mode == Mode.INSERT) {
Expand All @@ -97,12 +112,17 @@ public class Scratch.Plugins.VimEmulation : Peas.ExtensionBase, Scratch.Services
}

if (mode == Mode.INSERT) {
action += event.str;
//NOTE event.str` is gone in Gtk4 so use a different method
var uc = (unichar) (Gdk.keyval_to_unicode (keyval));
if (uc.isprint ()) {
action += uc.to_string ();
}

return false;
}

// Parse commands
switch (event.keyval) {
switch (keyval) {
//numbers
case Gdk.Key.@1:
number += "1";
Expand Down
1 change: 0 additions & 1 deletion plugins/word-completion/completion-provider.vala
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ public class Scratch.Plugins.CompletionProvider : Gtk.SourceCompletionProvider,
return true;
}


private bool get_proposals (out GLib.List<Gtk.SourceCompletionItem>? props, bool no_minimum) {
string to_find = "";
Gtk.TextBuffer temp_buffer = buffer;
Expand Down
26 changes: 20 additions & 6 deletions plugins/word-completion/plugin.vala
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public class Scratch.Plugins.Completion : Peas.ExtensionBase, Scratch.Services.A
public Object object { owned get; set construct; }

private List<Gtk.SourceView> text_view_list = new List<Gtk.SourceView> ();
private Gtk.EventControllerKey key_controller;
public Euclide.Completion.Parser parser {get; private set;}
public Gtk.SourceView? current_view {get; private set;}
public Scratch.Services.Document current_document {get; private set;}
Expand Down Expand Up @@ -53,6 +54,13 @@ public class Scratch.Plugins.Completion : Peas.ExtensionBase, Scratch.Services.A
});

plugins.hook_document.connect (on_new_source_view);
plugins.hook_window.connect ((w) => {
key_controller = new Gtk.EventControllerKey (w) {
propagation_phase = CAPTURE
};

key_controller.key_pressed.connect (on_key_press);
});
}

public void deactivate () {
Expand All @@ -78,7 +86,7 @@ public class Scratch.Plugins.Completion : Peas.ExtensionBase, Scratch.Services.A

current_document = doc;
current_view = doc.source_view;
current_view.key_press_event.connect (on_key_press);

current_view.completion.show.connect (() => {
completion_in_progress = true;
});
Expand Down Expand Up @@ -120,10 +128,18 @@ public class Scratch.Plugins.Completion : Peas.ExtensionBase, Scratch.Services.A
return false;
}

private bool on_key_press (Gtk.Widget view, Gdk.EventKey event) {
var kv = event.keyval;
private bool on_key_press (
uint keyval,
uint keycode,
Gdk.ModifierType state
) requires (current_view != null) {

var kv = keyval;
if (!current_view.is_focus) {
return false;
}
/* Pass through any modified keypress except Shift or Capslock */
Gdk.ModifierType mods = event.state & Gdk.ModifierType.MODIFIER_MASK
Gdk.ModifierType mods = state & Gdk.ModifierType.MODIFIER_MASK
& ~Gdk.ModifierType.SHIFT_MASK
& ~Gdk.ModifierType.LOCK_MASK;
if (mods > 0 ) {
Expand Down Expand Up @@ -166,8 +182,6 @@ public class Scratch.Plugins.Completion : Peas.ExtensionBase, Scratch.Services.A
}

private void cleanup (Gtk.SourceView view) {
current_view.key_press_event.disconnect (on_key_press);

current_view.completion.get_providers ().foreach ((p) => {
try {
/* Only remove provider added by this plug in */
Expand Down
Loading