Skip to content

Commit

Permalink
WIP: Edit existing sidebar item
Browse files Browse the repository at this point in the history
  • Loading branch information
simonhong committed Aug 25, 2022
1 parent fc45715 commit 080586c
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 0 deletions.
3 changes: 3 additions & 0 deletions app/brave_generated_resources.grd
Original file line number Diff line number Diff line change
Expand Up @@ -647,6 +647,9 @@ Or change later at <ph name="SETTINGS_EXTENIONS_LINK">$2<ex>brave://settings/ext

<!-- Sidebar -->
<if expr="enable_sidebar">
<message name="IDS_SIDEBAR_ITEM_CONTEXT_MENU_EDIT" desc="Menu for editing an existing item from sidebar">
Edit
</message>
<message name="IDS_SIDEBAR_ITEM_CONTEXT_MENU_REMOVE" desc="Menu for removing an item from sidebar">
Remove
</message>
Expand Down
4 changes: 4 additions & 0 deletions browser/ui/sidebar/sidebar_model.cc
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ void SidebarModel::OnItemMoved(const SidebarItem& item, int from, int to) {
UpdateActiveIndexAndNotify(new_active_index);
}

void SidebarModel::OnItemUpdated(const SidebarItem& item, int index) {
FetchFavicon(item);
}

void SidebarModel::OnWillRemoveItem(const SidebarItem& item, int index) {
if (index == active_index_)
UpdateActiveIndexAndNotify(-1);
Expand Down
1 change: 1 addition & 0 deletions browser/ui/sidebar/sidebar_model.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ class SidebarModel : public SidebarService::Observer,
// SidebarService::Observer overrides:
void OnItemAdded(const SidebarItem& item, int index) override;
void OnItemMoved(const SidebarItem& item, int from, int to) override;
void OnItemUpdated(const SidebarItem& item, int index) override;
void OnWillRemoveItem(const SidebarItem& item, int index) override;
void OnItemRemoved(const SidebarItem& item, int index) override;

Expand Down
10 changes: 10 additions & 0 deletions browser/ui/views/sidebar/sidebar_items_contents_view.cc
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,11 @@ void SidebarItemsContentsView::ShowContextMenuForViewImpl(
if (const ui::ColorProvider* color_provider = GetColorProvider()) {
icon_color = color_provider->GetColor(kColorSidebarButtonBase);
}
context_menu_model_->AddItemWithIcon(
kItemEdit,
brave_l10n::GetLocalizedResourceUTF16String(
IDS_SIDEBAR_ITEM_CONTEXT_MENU_EDIT),
ui::ImageModel::FromVectorIcon(kSidebarTrashIcon, icon_color));
context_menu_model_->AddItemWithIcon(
kItemRemove,
brave_l10n::GetLocalizedResourceUTF16String(
Expand All @@ -185,6 +190,11 @@ void SidebarItemsContentsView::ExecuteCommand(int command_id, int event_flags) {
GetSidebarService(browser_)->RemoveItemAt(index);
return;
}

if (command_id == kItemEdit) {
// TODO(simonhong): Launch edit dialog.
return;
}
}

void SidebarItemsContentsView::OnContextMenuClosed() {
Expand Down
1 change: 1 addition & 0 deletions browser/ui/views/sidebar/sidebar_items_contents_view.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ class SidebarItemsContentsView : public views::View,
friend class sidebar::SidebarBrowserTest;

enum ContextMenuIDs {
kItemEdit,
kItemRemove,
};

Expand Down
17 changes: 17 additions & 0 deletions components/sidebar/sidebar_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,23 @@ void SidebarService::MoveItem(int from, int to) {
UpdateSidebarItemsToPrefStore();
}

void SidebarService::UpdateItemAt(size_t index,
const GURL& url,
const std::u16string& title) {
DCHECK(items_.size() > index && index >= 0);
DCHECK_EQ(SidebarItem::Type::kTypeWeb, items_[index].type);
// Check |url| should be unique.

auto& item = items_[index];
item.url = url;
item.title = title;

for (Observer& obs : observers_)
obs.OnItemUpdated(item, index);

UpdateSidebarItemsToPrefStore();
}

void SidebarService::UpdateSidebarItemsToPrefStore() {
// Store all items in a list pref.
// Each item gets an entry. Built in items only need their type, and are
Expand Down
4 changes: 4 additions & 0 deletions components/sidebar/sidebar_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class SidebarService : public KeyedService {
virtual void OnItemMoved(const SidebarItem& item, int from, int to) {}
virtual void OnWillRemoveItem(const SidebarItem& item, int index) {}
virtual void OnItemRemoved(const SidebarItem& item, int index) {}
virtual void OnItemUpdated(const SidebarItem& item, int index) {}
virtual void OnShowSidebarOptionChanged(ShowSidebarOption option) {}

protected:
Expand All @@ -57,6 +58,9 @@ class SidebarService : public KeyedService {
void RemoveItemAt(int index);
void MoveItem(int from, int to);

// Only non-builtin type is editable.
void UpdateItemAt(size_t index, const GURL& url, const std::u16string& title);

void AddObserver(Observer* observer);
void RemoveObserver(Observer* observer);

Expand Down

0 comments on commit 080586c

Please sign in to comment.