Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(452)

Side by Side Diff: ash/shelf/shelf_view.cc

Issue 1760743002: Add simple mash context menu support. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase; use test items; cleanup. Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "ash/shelf/shelf_view.h" 5 #include "ash/shelf/shelf_view.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 8
9 #include "ash/ash_constants.h" 9 #include "ash/ash_constants.h"
10 #include "ash/ash_switches.h" 10 #include "ash/ash_switches.h"
(...skipping 1745 matching lines...) Expand 10 before | Expand all | Expand 10 after
1756 ->ItemSelected(event); 1756 ->ItemSelected(event);
1757 1757
1758 shelf_button_pressed_metric_tracker_.ButtonPressed(event, sender, 1758 shelf_button_pressed_metric_tracker_.ButtonPressed(event, sender,
1759 performed_action); 1759 performed_action);
1760 1760
1761 if (performed_action != ShelfItemDelegate::kNewWindowCreated) 1761 if (performed_action != ShelfItemDelegate::kNewWindowCreated)
1762 ShowListMenuForView(model_->items()[view_index], sender, event); 1762 ShowListMenuForView(model_->items()[view_index], sender, event);
1763 } 1763 }
1764 } 1764 }
1765 1765
1766 bool ShelfView::ShowListMenuForView(const ShelfItem& item, 1766 void ShelfView::ShowListMenuForView(const ShelfItem& item,
1767 views::View* source, 1767 views::View* source,
1768 const ui::Event& event) { 1768 const ui::Event& event) {
1769 ShelfItemDelegate* item_delegate = 1769 ShelfItemDelegate* item_delegate =
1770 item_manager_->GetShelfItemDelegate(item.id); 1770 item_manager_->GetShelfItemDelegate(item.id);
1771 scoped_ptr<ui::MenuModel> list_menu_model( 1771 scoped_ptr<ui::MenuModel> list_menu_model(
1772 item_delegate->CreateApplicationMenu(event.flags())); 1772 item_delegate->CreateApplicationMenu(event.flags()));
1773 1773
1774 // Make sure we have a menu and it has at least two items in addition to the 1774 // Make sure we have a menu and it has at least two items in addition to the
1775 // application title and the 3 spacing separators. 1775 // application title and the 3 spacing separators.
1776 if (!list_menu_model.get() || list_menu_model->GetItemCount() <= 5) 1776 if (!list_menu_model.get() || list_menu_model->GetItemCount() <= 5)
1777 return false; 1777 return;
1778 1778
1779 ShowMenu(list_menu_model.get(), 1779 ShowMenu(list_menu_model.get(), source, gfx::Point(), false,
1780 source,
1781 gfx::Point(),
1782 false,
1783 ui::GetMenuSourceTypeForEvent(event)); 1780 ui::GetMenuSourceTypeForEvent(event));
1784 return true;
1785 } 1781 }
1786 1782
1787 void ShelfView::ShowContextMenuForView(views::View* source, 1783 void ShelfView::ShowContextMenuForView(views::View* source,
1788 const gfx::Point& point, 1784 const gfx::Point& point,
1789 ui::MenuSourceType source_type) { 1785 ui::MenuSourceType source_type) {
1790 int view_index = view_model_->GetIndexOfView(source); 1786 const ShelfItem* item = ShelfItemForView(source);
1791 if (view_index == -1) { 1787 if (!item) {
1792 Shell::GetInstance()->ShowContextMenu(point, source_type); 1788 Shell::GetInstance()->ShowContextMenu(point, source_type);
1793 return; 1789 return;
1794 } 1790 }
1795 1791
1796 context_menu_model_.reset(Shell::GetInstance()->delegate()->CreateContextMenu( 1792 scoped_ptr<ui::MenuModel> context_menu_model(
1797 shelf_, &model_->items()[view_index])); 1793 Shell::GetInstance()->delegate()->CreateContextMenu(shelf_, item));
1798 if (!context_menu_model_) 1794 if (!context_menu_model)
1799 return; 1795 return;
1800 1796
1801 base::AutoReset<ShelfID> reseter( 1797 base::AutoReset<ShelfID> reseter(&context_menu_id_, item ? item->id : 0);
sky 2016/03/18 20:30:57 You shouldn't use AutoReset here. It's possible fo
msw 2016/03/18 22:10:30 AFAICT, we don't need the AutoReset and can just s
1802 &context_menu_id_, 1798 ShowMenu(context_menu_model.get(), source, point, true, source_type);
1803 view_index == -1 ? 0 : model_->items()[view_index].id);
1804
1805 ShowMenu(context_menu_model_.get(),
1806 source,
1807 point,
1808 true,
1809 source_type);
1810 } 1799 }
1811 1800
1812 void ShelfView::ShowMenu(ui::MenuModel* menu_model, 1801 void ShelfView::ShowMenu(ui::MenuModel* menu_model,
1813 views::View* source, 1802 views::View* source,
1814 const gfx::Point& click_point, 1803 const gfx::Point& click_point,
1815 bool context_menu, 1804 bool context_menu,
1816 ui::MenuSourceType source_type) { 1805 ui::MenuSourceType source_type) {
1817 closing_event_time_ = base::TimeDelta(); 1806 closing_event_time_ = base::TimeDelta();
1818 launcher_menu_runner_.reset(new views::MenuRunner( 1807 launcher_menu_runner_.reset(new views::MenuRunner(
1819 menu_model, context_menu ? views::MenuRunner::CONTEXT_MENU : 0)); 1808 menu_model, context_menu ? views::MenuRunner::CONTEXT_MENU : 0));
1820 1809
1821 ScopedTargetRootWindow scoped_target( 1810 aura::Window* window = source->GetWidget()->GetNativeWindow();
1822 source->GetWidget()->GetNativeView()->GetRootWindow()); 1811 ScopedTargetRootWindow scoped_target(window->GetRootWindow());
1823 1812
1824 // Determine the menu alignment dependent on the shelf.
1825 views::MenuAnchorPosition menu_alignment = views::MENU_ANCHOR_TOPLEFT; 1813 views::MenuAnchorPosition menu_alignment = views::MENU_ANCHOR_TOPLEFT;
1826 gfx::Rect anchor_point = gfx::Rect(click_point, gfx::Size()); 1814 gfx::Rect anchor = gfx::Rect(click_point, gfx::Size());
1827 1815
1828 if (!context_menu) { 1816 if (!context_menu) {
1829 // Application lists use a bubble. 1817 // Application lists use a bubble.
1830 ShelfAlignment align = shelf_->alignment();
1831 anchor_point = source->GetBoundsInScreen();
1832
1833 // It is possible to invoke the menu while it is sliding into view. To cover 1818 // It is possible to invoke the menu while it is sliding into view. To cover
1834 // that case, the screen coordinates are offsetted by the animation delta. 1819 // that case, the screen coordinates are offsetted by the animation delta.
1835 gfx::Vector2d offset = 1820 anchor = source->GetBoundsInScreen() +
1836 source->GetWidget()->GetNativeWindow()->bounds().origin() - 1821 (window->GetTargetBounds().origin() - window->bounds().origin());
1837 source->GetWidget()->GetNativeWindow()->GetTargetBounds().origin();
1838 anchor_point.set_x(anchor_point.x() - offset.x());
1839 anchor_point.set_y(anchor_point.y() - offset.y());
1840 1822
1841 // Shelf items can have an asymmetrical border for spacing reasons. 1823 // Adjust the anchor location for shelf items with asymmetrical borders.
1842 // Adjust anchor location for this.
1843 if (source->border()) 1824 if (source->border())
1844 anchor_point.Inset(source->border()->GetInsets()); 1825 anchor.Inset(source->border()->GetInsets());
1845 1826
1846 switch (align) { 1827 // Determine the menu alignment dependent on the shelf.
1847 case SHELF_ALIGNMENT_BOTTOM: 1828 menu_alignment = shelf_->SelectValueForShelfAlignment(
1848 menu_alignment = views::MENU_ANCHOR_BUBBLE_ABOVE; 1829 views::MENU_ANCHOR_BUBBLE_ABOVE, views::MENU_ANCHOR_BUBBLE_RIGHT,
1849 break; 1830 views::MENU_ANCHOR_BUBBLE_LEFT, views::MENU_ANCHOR_BUBBLE_BELOW);
1850 case SHELF_ALIGNMENT_LEFT:
1851 menu_alignment = views::MENU_ANCHOR_BUBBLE_RIGHT;
1852 break;
1853 case SHELF_ALIGNMENT_RIGHT:
1854 menu_alignment = views::MENU_ANCHOR_BUBBLE_LEFT;
1855 break;
1856 case SHELF_ALIGNMENT_TOP:
1857 menu_alignment = views::MENU_ANCHOR_BUBBLE_BELOW;
1858 break;
1859 }
1860 } 1831 }
1861 // If this gets deleted while we are in the menu, the shelf will be gone 1832 // If this is deleted while the menu is running, the shelf will also be gone.
1862 // as well.
1863 bool got_deleted = false; 1833 bool got_deleted = false;
1864 got_deleted_ = &got_deleted; 1834 got_deleted_ = &got_deleted;
1865 1835
1866 ShelfWidget* shelf_widget = shelf_->shelf_widget(); 1836 ShelfWidget* shelf_widget = shelf_->shelf_widget();
1867 shelf_widget->ForceUndimming(true); 1837 shelf_widget->ForceUndimming(true);
1868 // NOTE: if you convert to HAS_MNEMONICS be sure and update menu building 1838 // NOTE: if you convert to HAS_MNEMONICS be sure to update menu building code.
1869 // code. 1839 if (launcher_menu_runner_->RunMenuAt(source->GetWidget(), nullptr, anchor,
1870 if (launcher_menu_runner_->RunMenuAt(source->GetWidget(), 1840 menu_alignment, source_type) ==
1871 NULL,
1872 anchor_point,
1873 menu_alignment,
1874 source_type) ==
1875 views::MenuRunner::MENU_DELETED) { 1841 views::MenuRunner::MENU_DELETED) {
1876 if (!got_deleted) { 1842 if (!got_deleted) {
1877 got_deleted_ = NULL; 1843 got_deleted_ = NULL;
1878 shelf_widget->ForceUndimming(false); 1844 shelf_widget->ForceUndimming(false);
1879 } 1845 }
1880 return; 1846 return;
1881 } 1847 }
1882 got_deleted_ = NULL; 1848 got_deleted_ = NULL;
1883 shelf_widget->ForceUndimming(false); 1849 shelf_widget->ForceUndimming(false);
1884 1850
1885 // If it is a context menu and we are showing overflow bubble 1851 // Hide the hide overflow bubble after showing a context menu for its items.
1886 // we want to hide overflow bubble.
1887 if (owner_overflow_bubble_) 1852 if (owner_overflow_bubble_)
1888 owner_overflow_bubble_->HideBubbleAndRefreshButton(); 1853 owner_overflow_bubble_->HideBubbleAndRefreshButton();
1889 1854
1890 // Unpinning an item will reset the |launcher_menu_runner_| before coming 1855 // Unpinning an item will reset |launcher_menu_runner_| before coming here.
1891 // here.
1892 if (launcher_menu_runner_) 1856 if (launcher_menu_runner_)
1893 closing_event_time_ = launcher_menu_runner_->closing_event_time(); 1857 closing_event_time_ = launcher_menu_runner_->closing_event_time();
1894 Shell::GetInstance()->UpdateShelfVisibility(); 1858 Shell::GetInstance()->UpdateShelfVisibility();
1895 } 1859 }
1896 1860
1897 void ShelfView::OnBoundsAnimatorProgressed(views::BoundsAnimator* animator) { 1861 void ShelfView::OnBoundsAnimatorProgressed(views::BoundsAnimator* animator) {
1898 FOR_EACH_OBSERVER(ShelfIconObserver, observers_, 1862 FOR_EACH_OBSERVER(ShelfIconObserver, observers_,
1899 OnShelfIconPositionsChanged()); 1863 OnShelfIconPositionsChanged());
1900 PreferredSizeChanged(); 1864 PreferredSizeChanged();
1901 } 1865 }
(...skipping 25 matching lines...) Expand all
1927 1891
1928 base::TimeDelta delta = 1892 base::TimeDelta delta =
1929 base::TimeDelta(event.time_stamp() - closing_event_time_); 1893 base::TimeDelta(event.time_stamp() - closing_event_time_);
1930 closing_event_time_ = base::TimeDelta(); 1894 closing_event_time_ = base::TimeDelta();
1931 // If the current (press down) event is a repost event, the time stamp of 1895 // If the current (press down) event is a repost event, the time stamp of
1932 // these two events should be the same. 1896 // these two events should be the same.
1933 return (delta.InMilliseconds() == 0); 1897 return (delta.InMilliseconds() == 0);
1934 } 1898 }
1935 1899
1936 const ShelfItem* ShelfView::ShelfItemForView(const views::View* view) const { 1900 const ShelfItem* ShelfView::ShelfItemForView(const views::View* view) const {
1937 int view_index = view_model_->GetIndexOfView(view); 1901 const int view_index = view_model_->GetIndexOfView(view);
1938 if (view_index == -1) 1902 return (view_index < 0) ? nullptr : &(model_->items()[view_index]);
1939 return NULL;
1940 return &(model_->items()[view_index]);
1941 } 1903 }
1942 1904
1943 bool ShelfView::ShouldShowTooltipForView(const views::View* view) const { 1905 bool ShelfView::ShouldShowTooltipForView(const views::View* view) const {
1944 if (view == GetAppListButtonView() && 1906 if (view == GetAppListButtonView() &&
1945 Shell::GetInstance()->GetAppListWindow()) 1907 Shell::GetInstance()->GetAppListWindow())
1946 return false; 1908 return false;
1947 const ShelfItem* item = ShelfItemForView(view); 1909 const ShelfItem* item = ShelfItemForView(view);
1948 if (!item) 1910 if (!item)
1949 return true; 1911 return true;
1950 ShelfItemDelegate* item_delegate = 1912 ShelfItemDelegate* item_delegate =
1951 item_manager_->GetShelfItemDelegate(item->id); 1913 item_manager_->GetShelfItemDelegate(item->id);
1952 return item_delegate->ShouldShowTooltip(); 1914 return item_delegate->ShouldShowTooltip();
1953 } 1915 }
1954 1916
1955 int ShelfView::CalculateShelfDistance(const gfx::Point& coordinate) const { 1917 int ShelfView::CalculateShelfDistance(const gfx::Point& coordinate) const {
1956 const gfx::Rect bounds = GetBoundsInScreen(); 1918 const gfx::Rect bounds = GetBoundsInScreen();
1957 int distance = shelf_->SelectValueForShelfAlignment( 1919 int distance = shelf_->SelectValueForShelfAlignment(
1958 bounds.y() - coordinate.y(), coordinate.x() - bounds.right(), 1920 bounds.y() - coordinate.y(), coordinate.x() - bounds.right(),
1959 bounds.x() - coordinate.x(), coordinate.y() - bounds.bottom()); 1921 bounds.x() - coordinate.x(), coordinate.y() - bounds.bottom());
1960 return distance > 0 ? distance : 0; 1922 return distance > 0 ? distance : 0;
1961 } 1923 }
1962 1924
1963 } // namespace ash 1925 } // namespace ash
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698