Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Corner Layout #740

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
135 changes: 135 additions & 0 deletions src/jarabe/desktop/favoriteslayout.py
Original file line number Diff line number Diff line change
Expand Up @@ -631,3 +631,138 @@ def sin(r):
return RingLayout._calculate_position(self, radius, icon_size, index,
children_count, width, height,
sin=sin, cos=cos)


class CornerLayout(ViewLayout):
"""Lay out icons in the upper left corner."""

__gtype_name__ = 'CornerLayout'

icon_name = 'view-corner'
"""Name of icon used in home view dropdown palette."""

key = 'corner-layout'
"""String used in profile to represent this view."""

# TRANS: label for the box layout in the favorites view
palette_name = _('Corner')
"""String used to identify this layout in home view dropdown palette."""

def __init__(self):
ViewLayout.__init__(self)
self._grid = None
self._width = 0
self._height = 0
self._owner_allocation = None

def setup(self, allocation, owner_icon, activity_icon=None):
if self._grid is not None:
if self._width == allocation.width and \
self._height == allocation.height:
return
self._width = allocation.width
self._height = allocation.height
self._grid = Grid(int(allocation.width / _CELL_SIZE),
int(allocation.height / _CELL_SIZE))
self._grid.connect(
'child-changed', self.__grid_child_changed_cb, allocation)

self._allocate_owner_icon(allocation, owner_icon, activity_icon)

owner_request = owner_icon.size_request()
owner_width, owner_height = owner_request.width, owner_request.height
self._owner_allocation = self._make_rect(
(allocation.width / 2) - owner_width / 2,
(allocation.height / 2) - owner_height / 2,
owner_width, owner_height)

def __grid_child_changed_cb(self, grid, child, allocation):
request = child.size_request()
rect = self._grid.get_child_rect(child)
child_allocation = Gdk.Rectangle()
child_allocation.x = int(round(rect.x * _CELL_SIZE))
child_allocation.y = int(round(rect.y * _CELL_SIZE)) + allocation.y
child_allocation.width = request.width
child_allocation.height = request.height
child.size_allocate(child_allocation)

def _calculate_icon_size(self, icons_count):
if icons_count > 400:
return 32
if icons_count > 300:
return 36
elif icons_count > 200:
return 48
else:
return 54

def _get_next_icon_coords(self, x, y, next_x):
x -= 1
y += 1

if x < 0:
next_x += 1
x = next_x
y = 0

return (x, y, next_x)

def _coords_to_pixels(self, x, y, width, height):
space = 5
border = 10

return (x * (width + space) + border, y * (height + space) + border)

def _make_rect(self, x, y, width, height):
rect = Gdk.Rectangle()
rect.x = x
rect.y = y
rect.width = width
rect.height = height

return rect

def allocate_children(self, allocation, children):
x, y = 0, 0
next_x = 0
last_x = 0
last_y = 0

_x, _y = 0, 0

icon_size = self._calculate_icon_size(len(children))

for n in range(len(children)):
child = children[n]

x, y = self._coords_to_pixels(_x, _y, icon_size, icon_size)

if x + icon_size > allocation.width:
_x = next_x - 1
_y += 1

if y + icon_size > allocation.height:
_x = next_x
next_x += 1
_y = 0

x, y = self._coords_to_pixels(_x, _y, icon_size, icon_size)
rect = self._make_rect(x, y, icon_size, icon_size)

while Gdk.rectangle_intersect(rect, self._owner_allocation)[0] or \
(x + icon_size > allocation.width) or \
(y + icon_size > allocation.height):

_x, _y, next_x = self._get_next_icon_coords(_x, _y, next_x)
x, y = self._coords_to_pixels(_x, _y, icon_size, icon_size)
rect = self._make_rect(x, y, icon_size, icon_size)

child.set_size(icon_size)
child_allocation = Gdk.Rectangle()
child_allocation.x = allocation.x + x
child_allocation.y = allocation.y + y
child_allocation.width = icon_size
child_allocation.height = icon_size
child.size_allocate(child_allocation)

_x, _y, next_x = self._get_next_icon_coords(_x, _y, next_x)
1 change: 1 addition & 0 deletions src/jarabe/desktop/favoritesview.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
# favoriteslayout.TriangleLayout,
# favoriteslayout.SunflowerLayout.key:
# favoriteslayout.SunflowerLayout,
# favoriteslayout.CornerLayout.key: favoriteslayout.CornerLayout,
favoriteslayout.RandomLayout.key: favoriteslayout.RandomLayout}
"""Map numeric layout identifiers to uninstantiated subclasses of
`FavoritesLayout` which implement the layouts. Additional information
Expand Down