From d2bc6f9058ee35876672ce5cc2add8f318977b95 Mon Sep 17 00:00:00 2001 From: fujiwarat Date: Wed, 25 Dec 2019 17:53:19 +0900 Subject: Enable input mode by input context without global engine Users request to keep input mode in each input context when they switch the input contexts. ibus-anthy will remeber the input modes by object paths. BUG=https://github.com/ibus/ibus/issues/1679 --- engine/python2/engine.py | 27 +++++++++++++++++++++++++-- engine/python3/engine.py | 27 +++++++++++++++++++++++++-- setup/python2/prefs.py | 48 ++++++++++++++++++++++++++++++++++++++++-------- setup/python3/prefs.py | 48 ++++++++++++++++++++++++++++++++++++++++-------- 4 files changed, 130 insertions(+), 20 deletions(-) diff --git a/engine/python2/engine.py b/engine/python2/engine.py index 4798504..9e5aa73 100644 --- a/engine/python2/engine.py +++ b/engine/python2/engine.py @@ -125,6 +125,7 @@ class Engine(IBus.EngineSimple): __keybind = {} __thumb = None __latin_with_shift = True + __use_global_engine = True def __init__(self, bus, object_path): super(Engine, self).__init__(engine_name="anthy", @@ -144,6 +145,7 @@ class Engine(IBus.EngineSimple): self.__prop_dict = {} self.__input_purpose = 0 self.__has_input_purpose = False + self.__input_mode_by_context = {} if hasattr(IBus, 'InputPurpose'): self.__has_input_purpose = True try: @@ -281,6 +283,10 @@ class Engine(IBus.EngineSimple): Engine.__input_mode = INPUT_MODE_HIRAGANA Engine.__input_mode = self.__prefs.get_value('common', 'input-mode') + self.__input_mode_by_context['default'] = Engine.__input_mode + Engine.__use_global_engine = self.__prefs.get_value( + 'core:general', + 'use-global-engine') if not self.__prefs.get_value('common', 'show-input-mode'): return @@ -1047,7 +1053,15 @@ class Engine(IBus.EngineSimple): def __rgb(self, r, g, b): return self.__argb(255, r, g, b) - def do_focus_in(self): + def do_focus_in_with_path(self, object_path): + object_path = path.basename(object_path) + if not self.__use_global_engine: + if object_path in self.__input_mode_by_context: + mode = self.__input_mode_by_context[object_path] + Engine.__input_mode = mode + else: + Engine.__input_mode = self.__input_mode_by_context['default'] + self.__input_mode_by_context[object_path] = Engine.__input_mode self.register_properties(self.__prop_list) self.__refresh_typing_mode_property() mode = self.__prefs.get_value('common', 'behavior-on-focus-out') @@ -1059,7 +1073,10 @@ class Engine(IBus.EngineSimple): if size != self.__lookup_table.get_page_size(): self.__lookup_table.set_page_size(size) - def do_focus_out(self): + def do_focus_out_with_path(self, object_path): + object_path = path.basename(object_path) + if not self.__use_global_engine: + self.__input_mode_by_context[object_path] = Engine.__input_mode if self.__has_input_purpose: self.__input_purpose = 0 mode = self.__prefs.get_value('common', 'behavior-on-focus-out') @@ -1679,6 +1696,12 @@ class Engine(IBus.EngineSimple): def CONFIG_VALUE_CHANGED(cls, prefs, section, key, variant): if config.DEBUG: print('VALUE_CHAMGED =', section, key, variant) + is_core = section.startswith('core:') + if is_core: + if section == 'core:general': + if key == 'use-global-engine': + Engine.__use_global_engine = prefs.get_value(section, key) + return if section == 'shortcut': cls.__keybind = cls._mk_keybind() elif section == 'common': diff --git a/engine/python3/engine.py b/engine/python3/engine.py index 27f19c2..ba65e52 100644 --- a/engine/python3/engine.py +++ b/engine/python3/engine.py @@ -126,6 +126,7 @@ class Engine(IBus.EngineSimple): __keybind = {} __thumb = None __latin_with_shift = True + __use_global_engine = True def __init__(self, bus, object_path): super(Engine, self).__init__(engine_name="anthy", @@ -145,6 +146,7 @@ class Engine(IBus.EngineSimple): self.__prop_dict = {} self.__input_purpose = 0 self.__has_input_purpose = False + self.__input_mode_by_context = {} if hasattr(IBus, 'InputPurpose'): self.__has_input_purpose = True try: @@ -282,6 +284,10 @@ class Engine(IBus.EngineSimple): Engine.__input_mode = INPUT_MODE_HIRAGANA Engine.__input_mode = self.__prefs.get_value('common', 'input-mode') + self.__input_mode_by_context['default'] = Engine.__input_mode + Engine.__use_global_engine = self.__prefs.get_value( + 'core:general', + 'use-global-engine') if not self.__prefs.get_value('common', 'show-input-mode'): return @@ -1042,7 +1048,15 @@ class Engine(IBus.EngineSimple): def __rgb(self, r, g, b): return self.__argb(255, r, g, b) - def do_focus_in(self): + def do_focus_in_with_path(self, object_path): + object_path = path.basename(object_path) + if not self.__use_global_engine: + if object_path in self.__input_mode_by_context: + mode = self.__input_mode_by_context[object_path] + Engine.__input_mode = mode + else: + Engine.__input_mode = self.__input_mode_by_context['default'] + self.__input_mode_by_context[object_path] = Engine.__input_mode self.register_properties(self.__prop_list) self.__refresh_typing_mode_property() mode = self.__prefs.get_value('common', 'behavior-on-focus-out') @@ -1054,7 +1068,10 @@ class Engine(IBus.EngineSimple): if size != self.__lookup_table.get_page_size(): self.__lookup_table.set_page_size(size) - def do_focus_out(self): + def do_focus_out_with_path(self, object_path): + object_path = path.basename(object_path) + if not self.__use_global_engine: + self.__input_mode_by_context[object_path] = Engine.__input_mode if self.__has_input_purpose: self.__input_purpose = 0 mode = self.__prefs.get_value('common', 'behavior-on-focus-out') @@ -1674,6 +1691,12 @@ class Engine(IBus.EngineSimple): def CONFIG_VALUE_CHANGED(cls, prefs, section, key, variant): if config.DEBUG: print('VALUE_CHAMGED =', section, key, variant) + is_core = section.startswith('core:') + if is_core: + if section == 'core:general': + if key == 'use-global-engine': + Engine.__use_global_engine = prefs.get_value(section, key) + return if section == 'shortcut': cls.__keybind = cls._mk_keybind() elif section == 'common': diff --git a/setup/python2/prefs.py b/setup/python2/prefs.py index 5429f10..e6b991c 100644 --- a/setup/python2/prefs.py +++ b/setup/python2/prefs.py @@ -4,7 +4,7 @@ # # Copyright (c) 2007-2008 Peng Huang # Copyright (c) 2009 Hideaki ABE -# Copyright (c) 2010-2017 Takao Fujiwara +# Copyright (c) 2010-2019 Takao Fujiwara # Copyright (c) 2007-2017 Red Hat, Inc. # # This program is free software; you can redistribute it and/or modify @@ -109,15 +109,36 @@ class Prefs(GObject.GObject): schema=self.__schema_prefix + section) self.__settings[section].connect('changed', self.__settings_on_changed) + self.__core_cache = {} + self.__core_settings = {} + self.__core_schema_prefix = 'org.freedesktop.ibus.' + self.__core_schema_sections = ['general'] + for section in self.__core_schema_sections: + self.__core_settings[section] = Gio.Settings( + schema=self.__core_schema_prefix + section) + self.__core_settings[section].connect('changed', + self.__settings_on_changed) def __settings_on_changed(self, settings, key): - section = settings.props.schema[len(self.__schema_prefix):] - variant_value = self.__settings[section].get_value(key) - variant_key = self.__cache.get(section) + schema = settings.props.schema + is_anthy = False + if schema.startswith(self.__schema_prefix): + section = settings.props.schema[len(self.__schema_prefix):] + variant_value = self.__settings[section].get_value(key) + variant_key = self.__cache.get(section) + is_anthy = True + else: + section = settings.props.schema[len(self.__core_schema_prefix):] + variant_value = self.__core_settings[section].get_value(key) + variant_key = self.__core_cache.get(section) if variant_key == None: variant_key = {} variant_key[key] = variant_value - self.__cache[section] = variant_key + if is_anthy: + self.__cache[section] = variant_key + else: + self.__core_cache[section] = variant_key + section = 'core:' + section self.emit('changed', section, key, variant_value) def variant_to_value(self, variant): @@ -158,16 +179,27 @@ class Prefs(GObject.GObject): return variant def get_variant(self, section, key): - variant_key = self.__cache.get(section) + is_core = section.startswith('core:') + if is_core: + section = section[len('core:'):] + variant_key = self.__core_cache.get(section) + else: + variant_key = self.__cache.get(section) if variant_key != None: variant_value = variant_key.get(key) if variant_value != None: return variant_value - variant_value = self.__settings[section].get_value(key) + if is_core: + variant_value = self.__core_settings[section].get_value(key) + else: + variant_value = self.__settings[section].get_value(key) if variant_key == None: variant_key = {} variant_key[key] = variant_value - self.__cache[section] = variant_key + if is_core: + self.__core_cache[section] = variant_key + else: + self.__cache[section] = variant_key return variant_value def get_default_variant(self, section, key): diff --git a/setup/python3/prefs.py b/setup/python3/prefs.py index 6deb32b..2864d63 100644 --- a/setup/python3/prefs.py +++ b/setup/python3/prefs.py @@ -4,7 +4,7 @@ # # Copyright (c) 2007-2008 Peng Huang # Copyright (c) 2009 Hideaki ABE -# Copyright (c) 2010-2017 Takao Fujiwara +# Copyright (c) 2010-2019 Takao Fujiwara # Copyright (c) 2007-2017 Red Hat, Inc. # # This program is free software; you can redistribute it and/or modify @@ -109,15 +109,36 @@ class Prefs(GObject.GObject): schema=self.__schema_prefix + section) self.__settings[section].connect('changed', self.__settings_on_changed) + self.__core_cache = {} + self.__core_settings = {} + self.__core_schema_prefix = 'org.freedesktop.ibus.' + self.__core_schema_sections = ['general'] + for section in self.__core_schema_sections: + self.__core_settings[section] = Gio.Settings( + schema=self.__core_schema_prefix + section) + self.__core_settings[section].connect('changed', + self.__settings_on_changed) def __settings_on_changed(self, settings, key): - section = settings.props.schema[len(self.__schema_prefix):] - variant_value = self.__settings[section].get_value(key) - variant_key = self.__cache.get(section) + schema = settings.props.schema + is_anthy = False + if schema.startswith(self.__schema_prefix): + section = settings.props.schema[len(self.__schema_prefix):] + variant_value = self.__settings[section].get_value(key) + variant_key = self.__cache.get(section) + is_anthy = True + else: + section = settings.props.schema[len(self.__core_schema_prefix):] + variant_value = self.__core_settings[section].get_value(key) + variant_key = self.__core_cache.get(section) if variant_key == None: variant_key = {} variant_key[key] = variant_value - self.__cache[section] = variant_key + if is_anthy: + self.__cache[section] = variant_key + else: + self.__core_cache[section] = variant_key + section = 'core:' + section self.emit('changed', section, key, variant_value) def variant_to_value(self, variant): @@ -158,16 +179,27 @@ class Prefs(GObject.GObject): return variant def get_variant(self, section, key): - variant_key = self.__cache.get(section) + is_core = section.startswith('core:') + if is_core: + section = section[len('core:'):] + variant_key = self.__core_cache.get(section) + else: + variant_key = self.__cache.get(section) if variant_key != None: variant_value = variant_key.get(key) if variant_value != None: return variant_value - variant_value = self.__settings[section].get_value(key) + if is_core: + variant_value = self.__core_settings[section].get_value(key) + else: + variant_value = self.__settings[section].get_value(key) if variant_key == None: variant_key = {} variant_key[key] = variant_value - self.__cache[section] = variant_key + if is_core: + self.__core_cache[section] = variant_key + else: + self.__cache[section] = variant_key return variant_value def get_default_variant(self, section, key): -- cgit v1.2.1