1# ARandR -- Another XRandR GUI
2# Copyright (C) 2008 -- 2011 chrysn <chrysn@fsfe.org>
3#
4# This program is free software: you can redistribute it and/or modify
5# it under the terms of the GNU General Public License as published by
6# the Free Software Foundation, either version 3 of the License, or
7# (at your option) any later version.
8#
9# This program is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12# GNU General Public License for more details.
13#
14# You should have received a copy of the GNU General Public License
15# along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17from .auxiliary import Position
18
19
20class Snap:
21    """Snap-to-edges manager"""
22
23    def __init__(self, size, tolerance, positions):
24        self.tolerance = tolerance
25
26        self.horizontal = set()
27        self.vertical = set()
28        for i in positions:
29            self.vertical.add(i[0].left)
30            self.vertical.add(i[0].left + i[1].width)
31            self.horizontal.add(i[0].top)
32            self.horizontal.add(i[0].top + i[1].height)
33
34            self.vertical.add(i[0].left - size.width)
35            self.vertical.add(i[0].left + i[1].width - size.width)
36            self.horizontal.add(i[0].top - size.height)
37            self.horizontal.add(i[0].top + i[1].height - size.height)
38
39            self.vertical.add((i[0].left + i[1].width / 2) - size.width / 2)
40            self.horizontal.add((i[0].top + i[1].height / 2) - size.height / 2)
41
42    def suggest(self, position):
43        vertical = [x for x in self.vertical if abs(
44            x - position[0]) < self.tolerance]
45        horizontal = [y for y in self.horizontal if abs(
46            y - position[1]) < self.tolerance]
47
48        if vertical:
49            position = Position((vertical[0], position[1]))
50        if horizontal:
51            position = Position((position[0], horizontal[0]))
52
53        return position
54