Marzypan

extension.js

Feb 22nd, 2021 (edited)
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // SwitchFocusMode extension (c) 2014 Romano Giannetti <[email protected]>
  2. // License: GPLv2+, see http://www.gnu.org/licenses/gpl-2.0.txt
  3. //
  4. // Configuration: choose the kind of FFM you like, be "sloppy" or "mouse"
  5. const FFM_VARIANT='sloppy';
  6.  
  7. // End configuration
  8. const St = imports.gi.St;
  9. const Main = imports.ui.main;
  10. const Tweener = imports.ui.tweener;
  11. const ExtensionUtils = imports.misc.extensionUtils;
  12. const Me = imports.misc.extensionUtils.getCurrentExtension();
  13. const {Gio, Shell, Meta} = imports.gi;
  14.  
  15. let text, button, icon_f, icon_c, wm_prefs;
  16.  
  17. function getSettings() {
  18.   let GioSSS = Gio.SettingsSchemaSource;
  19.   let schemaSource = GioSSS.new_from_directory(
  20.     Me.dir.get_child("schemas").get_path(),
  21.     GioSSS.get_default(),
  22.     false
  23.   );
  24.   let schemaObj = schemaSource.lookup(
  25.   'org.gnome.shell.extensions.switchfocustype', true);
  26.   if (!schemaObj) {
  27.   throw new Error('cannot find schemas');
  28.   }
  29.   return new Gio.Settings({ settings_schema : schemaObj });
  30.   }
  31.  
  32. function _hideMsg() {
  33.     if (text) {
  34.         Main.uiGroup.remove_actor(text);
  35.         text = null;
  36.     }
  37. }
  38.  
  39. function _showMsg(what) {
  40.     _hideMsg(); // in case it's still linging there
  41.     if (!text) {
  42.         text = new St.Label({ style_class: 'msg-label', text: what });
  43.         Main.uiGroup.add_actor(text);
  44.     }
  45.  
  46.     text.opacity = 255;
  47.     let monitor = Main.layoutManager.primaryMonitor;
  48.     text.set_position(Math.floor(monitor.width / 2 - text.width / 2),
  49.             Math.floor(monitor.height / 2 - text.height / 2));
  50.     Tweener.addTween(text,
  51.             { opacity: 0,
  52.                 time: 3,
  53.         transition: 'easeOutQuad',
  54.         onComplete: _hideMsg });
  55. }
  56.  
  57. function _switch() {
  58.     let what=wm_prefs.get_string('focus-mode');
  59.     if (what == 'click') {
  60.         _showMsg("Setting Focus-follow-mouse");
  61.         button.set_child(icon_f);
  62.         wm_prefs.set_string('focus-mode', FFM_VARIANT);
  63.     } else { // sloppy or mouse
  64.         _showMsg("Setting Click-to-focus");
  65.         button.set_child(icon_c);
  66.         wm_prefs.set_string('focus-mode', 'click');
  67.     }
  68. }
  69.  
  70. function _sync() {
  71.     let what=wm_prefs.get_string('focus-mode');
  72.     if (what == 'click') {
  73.         button.set_child(icon_c);
  74.     } else { // sloppy or mouse
  75.         button.set_child(icon_f);
  76.     }
  77. }
  78.  
  79. function init() {
  80.     button = new St.Bin({ style_class: 'panel-button',
  81.         reactive: true,
  82.             can_focus: true,
  83.             x_fill: true,
  84.             y_fill: false,
  85.             track_hover: true });
  86.     icon_f = new St.Icon({ style_class: 'system-status-icon' });
  87.     icon_f.gicon = Gio.icon_new_for_string(Me.path + '/icons/fmode.svg');
  88.     icon_c = new St.Icon({ style_class: 'system-status-icon' });
  89.     icon_c.gicon = Gio.icon_new_for_string(Me.path + '/icons/cmode.svg');
  90.     wm_prefs=new Gio.Settings({schema: 'org.gnome.desktop.wm.preferences'});
  91.     button.connect('button-press-event', _switch);
  92. }
  93.  
  94. function enable() {
  95.     // start with the current mode --- sync icon
  96.     _sync();
  97.     Main.panel._rightBox.insert_child_at_index(button, 0);
  98.  
  99.   let mode = Shell.ActionMode.All;
  100.  
  101.   let flag = Meta.KeyBindingFlags.NONE;
  102.  
  103.   let settings = getSettings();
  104.  
  105.   Main.wm.addKeybinding("my-shortcut", settings, flag, mode, () => {
  106.     log('shortcut is working');
  107.   });
  108. }
  109.  
  110. function disable() {
  111.     Main.panel._rightBox.remove_child(button);
  112.     Main.wm.removeKeybinding("my-shortcut");
  113. }
  114.  
Advertisement
Add Comment
Please, Sign In to add comment