From 85ea8fe563d4d2177454c03e9c72bfa86c1c95b0 Mon Sep 17 00:00:00 2001 From: Sumner Evans Date: Sat, 6 Jun 2020 18:40:24 -0600 Subject: [PATCH] Made edit and add buttons work from menu --- sublime/adapters/configure_server_form.py | 2 +- sublime/app.py | 36 ++++++------- sublime/ui/configure_provider.py | 62 ++++++++++++++--------- sublime/ui/main.py | 28 +++------- 4 files changed, 66 insertions(+), 62 deletions(-) diff --git a/sublime/adapters/configure_server_form.py b/sublime/adapters/configure_server_form.py index f3d5d6f..1c643ad 100644 --- a/sublime/adapters/configure_server_form.py +++ b/sublime/adapters/configure_server_form.py @@ -7,7 +7,7 @@ from functools import partial from pathlib import Path from typing import Any, Callable, cast, Dict, Iterable, Optional, Tuple, Type, Union -from gi.repository import Gdk, GLib, GObject, Gtk, Pango +from gi.repository import GLib, GObject, Gtk, Pango from . import ConfigurationStore diff --git a/sublime/app.py b/sublime/app.py index a9e6bb3..2aa7781 100644 --- a/sublime/app.py +++ b/sublime/app.py @@ -74,10 +74,10 @@ class SublimeMusicApp(Gtk.Application): self.add_action(action) # Add action for menu items. - add_action("add-new-provider", self.on_add_new_provider) - add_action("edit-current-provider", self.on_edit_current_provider) - add_action("switch-provider", self.on_switch_provider) - add_action("remove-provider", self.on_remove_provider) + add_action("add-new-music-provider", self.on_add_new_music_provider) + add_action("edit-current-music-provider", self.on_edit_current_music_provider) + add_action("switch-music-provider", self.on_switch_music_provider) + add_action("remove-music-provider", self.on_remove_music_provider) # Add actions for player controls add_action("play-pause", self.on_play_pause) @@ -164,10 +164,6 @@ class SublimeMusicApp(Gtk.Application): self.window.close() return - self.app_config.current_provider_id = list( - self.app_config.providers.keys() - )[0] - AdapterManager.reset(self.app_config, self.on_song_download_progress) # Connect after we know there's a server configured. @@ -562,16 +558,18 @@ class SublimeMusicApp(Gtk.Application): self.reset_state() - def on_add_new_provider(self, _, provider: ProviderConfiguration): + def on_add_new_music_provider(self, *args): + self.show_configure_servers_dialog() + + def on_edit_current_music_provider(self, *args): + self.show_configure_servers_dialog(self.app_config.provider) + + def on_switch_music_provider(self, _, provider_id: str): + print("SWITCH") pass - def on_edit_current_provider(self, _): - pass - - def on_switch_provider(self, _, provider_id: str): - pass - - def on_remove_provider(self, _, provider_id: str): + def on_remove_music_provider(self, _, provider_id: str): + print("REMOVE") pass def on_window_go_to(self, win: Any, action: str, value: str): @@ -931,7 +929,12 @@ class SublimeMusicApp(Gtk.Application): assert dialog.provider_config is not None provider_id = dialog.provider_config.id self.app_config.providers[provider_id] = dialog.provider_config + + # Switch to the new provider. + self.app_config.current_provider_id = provider_id self.app_config.save() + self.update_window(force=True) + dialog.destroy() def update_window(self, force: bool = False): @@ -1300,7 +1303,6 @@ class SublimeMusicApp(Gtk.Application): ) def save_play_queue(self, song_playing_order_token: int = None): - print('SAVE PLAY QUEUE') if ( len(self.app_config.state.play_queue) == 0 or self.app_config.provider is None diff --git a/sublime/ui/configure_provider.py b/sublime/ui/configure_provider.py index f5ebe47..e4b885b 100644 --- a/sublime/ui/configure_provider.py +++ b/sublime/ui/configure_provider.py @@ -2,12 +2,11 @@ import uuid from enum import Enum from typing import Any, Optional, Type -from gi.repository import Gio, GLib, GObject, Gtk, Pango +from gi.repository import Gio, GObject, Gtk, Pango from sublime.adapters import AdapterManager, UIInfo from sublime.adapters.filesystem import FilesystemAdapter from sublime.config import ConfigurationStore, ProviderConfiguration -from sublime.ui.common import IconButton class AdapterTypeModel(GObject.GObject): @@ -40,32 +39,35 @@ class ConfigureProviderDialog(Gtk.Dialog): ), } + def set_title(self, editing: bool, provider_config: ProviderConfiguration = None): + if editing: + assert provider_config is not None + title = f"Edit {provider_config.name}" + else: + title = "Add New Music Source" + + self.header.props.title = title + def __init__(self, parent: Any, provider_config: Optional[ProviderConfiguration]): - title = ( - "Add New Music Source" - if not provider_config - else "Edit {provider_config.name}" - ) - Gtk.Dialog.__init__( - self, title=title, transient_for=parent, flags=Gtk.DialogFlags.MODAL - ) + Gtk.Dialog.__init__(self, transient_for=parent, flags=Gtk.DialogFlags.MODAL) # TODO esc should prompt or go back depending on the page self.provider_config = provider_config + self.editing = provider_config is not None self.set_default_size(400, 350) # HEADER - header = Gtk.HeaderBar() - header.props.title = title + self.header = Gtk.HeaderBar() + self.set_title(self.editing, provider_config) self.cancel_back_button = Gtk.Button(label="Cancel") self.cancel_back_button.connect("clicked", self._on_cancel_back_clicked) - header.pack_start(self.cancel_back_button) + self.header.pack_start(self.cancel_back_button) - self.next_add_button = Gtk.Button(label="Next") + self.next_add_button = Gtk.Button(label="Edit" if self.editing else "Next") self.next_add_button.connect("clicked", self._on_next_add_clicked) - header.pack_end(self.next_add_button) + self.header.pack_end(self.next_add_button) - self.set_titlebar(header) + self.set_titlebar(self.header) content_area = self.get_content_area() @@ -109,8 +111,6 @@ class ConfigureProviderDialog(Gtk.Dialog): available_ground_truth_adapters = filter( lambda a: a.can_be_ground_truth, AdapterManager.available_adapters ) - # TODO: DEBUG REMOVE NEXT LINE - available_ground_truth_adapters = AdapterManager.available_adapters for adapter_type in sorted( available_ground_truth_adapters, key=lambda a: a.get_ui_info().name ): @@ -126,6 +126,19 @@ class ConfigureProviderDialog(Gtk.Dialog): self.show_all() + if self.editing: + assert self.provider_config + for i, adapter_type in enumerate(self.adapter_type_store): + if ( + adapter_type.adapter_type + == self.provider_config.ground_truth_adapter_type + ): + row = self.adapter_options_list.get_row_at_index(i) + self.adapter_options_list.select_row(row) + break + self._name_is_valid = True + self._on_next_add_clicked() + def _on_cancel_back_clicked(self, _): if self.stage == DialogStage.SELECT_ADAPTER: self.close() @@ -175,12 +188,12 @@ class ConfigureProviderDialog(Gtk.Dialog): self.configure_box.pack_start(form, True, True, 0) self.configure_box.show_all() - self._current_index = index self.stack.set_visible_child_name("configure") self.stage = DialogStage.CONFIGURE_ADAPTER - self.cancel_back_button.set_label("Back") - self.next_add_button.set_label("Add") - self.next_add_button.set_sensitive(False) + self.cancel_back_button.set_label("Change Type" if self.editing else "Back") + self.next_add_button.set_label("Edit" if self.editing else "Add") + self.next_add_button.set_sensitive(index == self._current_index) + self._current_index = index else: if self.provider_config is None: self.provider_config = ProviderConfiguration( @@ -190,7 +203,6 @@ class ConfigureProviderDialog(Gtk.Dialog): self.config_store, ) if self.adapter_type.can_be_cached: - # TODO if we ever have more caching adapters, need to change this. self.provider_config.caching_adapter_type = FilesystemAdapter self.provider_config.caching_adapter_config = ConfigurationStore() else: @@ -212,6 +224,10 @@ class ConfigureProviderDialog(Gtk.Dialog): self._name_is_valid = True entry.get_style_context().remove_class("invalid") entry.set_tooltip_markup(None) + + assert self.provider_config + self.provider_config.name = entry.get_text() + self.set_title(self.editing, self.provider_config) else: self._name_is_valid = False entry.get_style_context().add_class("invalid") diff --git a/sublime/ui/main.py b/sublime/ui/main.py index 257b2d0..3cf76c8 100644 --- a/sublime/ui/main.py +++ b/sublime/ui/main.py @@ -452,12 +452,14 @@ class MainWindow(Gtk.ApplicationWindow): return box, switch def _create_model_button( - self, text: str, clicked_fn: Callable = None, **kwargs + self, text: str, clicked_fn: Callable = None, action_name: str = None, **kwargs ) -> Gtk.ModelButton: model_button = Gtk.ModelButton(text=text, **kwargs) model_button.get_style_context().add_class("menu-button") if clicked_fn: model_button.connect("clicked", clicked_fn) + if action_name: + model_button.set_action_name(f"app.{action_name}") return model_button def _create_spin_button_menu_item( @@ -526,7 +528,7 @@ class MainWindow(Gtk.ApplicationWindow): ("Delete Cached Song Files and Metadata", self._clear_entire_cache), ] for text, clicked_fn in menu_items: - clear_song_cache = self._create_model_button(text, clicked_fn) + clear_song_cache = self._create_model_button(text, clicked_fn=clicked_fn) clear_cache_options.pack_start(clear_song_cache, False, True, 0) menu.add(clear_cache_options) @@ -573,23 +575,19 @@ class MainWindow(Gtk.ApplicationWindow): vbox.add(offline_box) edit_button = self._create_model_button( - "Edit Configuration...", self._on_edit_configuration_click + "Edit Configuration...", action_name="edit-current-music-provider" ) vbox.add(edit_button) vbox.add(Gtk.Separator(orientation=Gtk.Orientation.HORIZONTAL)) music_provider_button = self._create_model_button( - "Switch Music Provider", - self._on_switch_provider_click, - menu_name="switch-provider", + "Switch Music Provider", menu_name="switch-provider", ) - # TODO (#197) - music_provider_button.set_action_name("app.configure-servers") vbox.add(music_provider_button) add_new_music_provider_button = self._create_model_button( - "Add New Music Provider...", self._on_add_new_provider_click + "Add New Music Provider...", action_name="add-new-music-provider" ) vbox.add(add_new_music_provider_button) @@ -817,18 +815,6 @@ class MainWindow(Gtk.ApplicationWindow): {"replay_gain": ReplayGainType.from_string(combo.get_active_id())} ) - def _on_edit_configuration_click(self, _): - # TODO (#197): EDIT - pass - - def _on_switch_provider_click(self, _): - # TODO (#197): switch - pass - - def _on_add_new_provider_click(self, _): - # TODO (#197) add new - pass - def _on_search_entry_focus(self, *args): self._show_search()