Exemplo n.º 1
0
    def load_settings(self):
        """Load settings from the Settings instance."""
        self._double_click_switch.set_active(Settings.get().double_click)
        self._live_search_switch.set_active(Settings.get().live_search)
        self._pronunciations_accent_row.set_selected(
            Settings.get().pronunciations_accent_value)

        self._dark_ui_switch.set_active(Settings.get().gtk_dark_ui)
Exemplo n.º 2
0
    def setup_widgets(self):
        """Setup the widgets in the window."""
        self._search_history = Gio.ListStore.new(HistoryObject)
        self._history_listbox.bind_model(self._search_history, self._create_label)

        self.connect("unrealize", self._on_destroy)
        self._key_ctrlr.connect("key-pressed", self._on_key_pressed)
        self._history_listbox.connect("row-activated", self._on_history_item_activated)

        self._def_ctrlr.connect("pressed", self._on_def_press_event)
        self._def_ctrlr.connect("stopped", self._on_def_stop_event)
        self._def_view.connect("activate-link", self._on_link_activated)
        self.search_button.connect("clicked", self.on_search_clicked)
        self._search_entry.connect("changed", self._on_entry_changed)
        self._speak_button.connect("clicked", self._on_speak_clicked)
        self._retry_button.connect("clicked", self._on_retry_clicked)
        self._exit_button.connect("clicked", self._on_exit_clicked)
        self._main_scroll.get_vadjustment().connect(
            "value-changed", self._on_scroll_event
        )

        self._flap.bind_property(
            "reveal-flap",
            self._flap_toggle_button,
            "active",
            GObject.BindingFlags.BIDIRECTIONAL | GObject.BindingFlags.SYNC_CREATE,
        )

        self._style_manager = self.get_application().get_style_manager()
        self._style_manager.connect("notify::dark", self._on_dark_style)

        # Loading and setup.
        self._dl_wn()
        if self._wn_downloader.check_status():
            self._wn_future = base.get_wn_file(self._retry_dl_wn)
            self._set_header_sensitive(True)
            self._page_switch(Page.WELCOME)
            if self.lookup_term:
                self.trigger_search(self.lookup_term)
            self._search_entry.grab_focus_without_selecting()

        # Completions
        self.completer = Gtk.EntryCompletion()
        self.completer.set_popup_single_match(False)
        self.completer.set_text_column(0)
        self.completer.set_popup_completion(not Settings.get().live_search)
        self.completer.set_popup_set_width(True)
        self._search_entry.set_completion(self.completer)

        # Load History.
        self._search_history_list = Settings.get().history
        for text in self._search_history_list:
            history_object = HistoryObject(text)
            self._search_history.insert(0, history_object)

        # Set search button visibility.
        self.search_button.set_visible(not Settings.get().live_search)
Exemplo n.º 3
0
 def _on_speak_clicked(self, _button):
     """Say the search entry out loud with espeak speech synthesis."""
     base.read_term(
         self._searched_term,
         speed="120",
         accent=Settings.get().pronunciations_accent,
     )
Exemplo n.º 4
0
    def _update_completions(self, text):
        """Update completions from wordlist and cdef folder."""
        while self._completion_request_count > 0:
            completer_liststore = Gtk.ListStore(str)
            _complete_list = []

            for item in self._wn_future.result()["list"]:
                if len(_complete_list) >= 10:
                    break
                item = item.replace("_", " ")
                if item.lower().startswith(text.lower()) and item not in _complete_list:
                    _complete_list.append(item)

            if Settings.get().cdef:
                for item in os.listdir(utils.CDEF_DIR):
                    # FIXME: There is no indicator that this is a custom definition
                    # Not a priority but a nice-to-have.
                    if len(_complete_list) >= 10:
                        break
                    item = escape(item).replace("_", " ")
                    if (
                        item.lower().startswith(text.lower())
                        and item not in _complete_list
                    ):
                        _complete_list.append(item)

            _complete_list = sorted(_complete_list, key=str.casefold)
            for item in _complete_list:
                completer_liststore.append((item,))

            self._completion_request_count -= 1
            GLib.idle_add(self.completer.set_model, completer_liststore)
            GLib.idle_add(self.completer.complete)
Exemplo n.º 5
0
 def _search(self, search_text):
     """Clean input text, give errors and pass data to reactor."""
     text = base.cleaner(search_text)
     if not text == "" and not text.isspace():
         return base.reactor(
             text,
             self._style_manager.get_dark(),
             self._wn_future.result()["instance"],
             Settings.get().cdef,
             accent=Settings.get().pronunciations_accent,
         )
     if not Settings.get().live_search:
         GLib.idle_add(
             self._new_error,
             _("Invalid input"),
             _("Nothing definable was found in your search input"),
         )
     self._searched_term = None
     return None
Exemplo n.º 6
0
    def _on_entry_changed(self, _entry):
        """Detect changes to text and do live search if enabled."""

        self._completion_request_count += 1
        if self._completion_request_count == 1:
            threading.Thread(
                target=self._update_completions,
                args=[self._search_entry.get_text()],
                daemon=True,
            ).start()

        if Settings.get().live_search:
            GLib.idle_add(self.on_search_clicked)
Exemplo n.º 7
0
    def __init__(self, app_id, version):
        """Initialize the application."""
        super().__init__(
            application_id=app_id,
            flags=Gio.ApplicationFlags.HANDLES_COMMAND_LINE,
        )
        GLib.set_application_name(_("Wordbook"))
        GLib.set_prgname(self.app_id)

        self.app_id = app_id
        self.version = version

        # Add command line options
        self.add_main_option(
            "look-up",
            b"l",
            GLib.OptionFlags.NONE,
            GLib.OptionArg.STRING,
            "Term to look up",
            None,
        )
        self.add_main_option(
            "info",
            ord("i"),
            GLib.OptionFlags.NONE,
            GLib.OptionArg.NONE,
            "Print version info",
            None,
        )
        self.add_main_option(
            "verbose",
            ord("v"),
            GLib.OptionFlags.NONE,
            GLib.OptionArg.NONE,
            "Make it scream louder",
            None,
        )

        Adw.StyleManager.get_default().set_color_scheme(
            Adw.ColorScheme.FORCE_DARK if Settings.get(
            ).gtk_dark_ui else Adw.ColorScheme.PREFER_LIGHT)

        base.fold_gen()
Exemplo n.º 8
0
 def _on_dark_ui_switch_activate(switch, _gparam):
     """Change UI theme."""
     Settings.get().gtk_dark_ui = switch.get_active()
     Adw.StyleManager.get_default().set_color_scheme(
         Adw.ColorScheme.FORCE_DARK if switch.get_active(
         ) else Adw.ColorScheme.PREFER_LIGHT)
Exemplo n.º 9
0
 def _on_pronunciations_accent_activate(row, _gparam):
     """Change pronunciations' accent."""
     Settings.get().pronunciations_accent_value = row.get_selected()
Exemplo n.º 10
0
 def _on_live_search_activate(self, switch, _gparam):
     """Change live search state."""
     self.parent.completer.set_popup_completion(not switch.get_active())
     self.parent.search_button.set_visible(not switch.get_active())
     Settings.get().live_search = switch.get_active()
Exemplo n.º 11
0
 def _double_click_switch_activate(switch, _gparam):
     """Change 'double click to search' state."""
     Settings.get().double_click = switch.get_active()
Exemplo n.º 12
0
 def _on_destroy(self, _window):
     """Detect closing of the window."""
     Settings.get().history = self._search_history_list[-10:]
Exemplo n.º 13
0
 def _on_def_press_event(self, _click, n_press, _x, _y):
     """Handle double click on definition view."""
     if Settings.get().double_click:
         self._doubled = n_press == 2
     else:
         self._doubled = False