/usr/lib/python3/dist-packages/Onboard/TouchHandles.py is in onboard 1.0.0-0ubuntu4.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 | # -*- coding: utf-8 -*-
""" Enlarged drag handles for resizing or moving """
from __future__ import division, print_function, unicode_literals
from math import pi, sqrt, sin, log
import cairo
from Onboard.utils import Rect, drop_shadow
from Onboard.WindowUtils import Handle
### Logging ###
import logging
from functools import reduce
_logger = logging.getLogger("TouchHandles")
###############
class TouchHandle(object):
""" Enlarged drag handle for resizing or moving """
id = None
prelight = False
pressed = False
corner_radius = 0 # radius of the outer corners (window edges)
_size = (40, 40)
_fallback_size = (40, 40)
_hit_proximity_factor = 1.5
_rect = None
_scale = 1.0 # scale of handle relative to resize handles
_handle_alpha = 0.45
_shadow_alpha = 0.04
_shadow_size = 8
_shadow_offset = (0.0, 2.0)
_screen_dpi = 96
_handle_angles = {} # dictionary at class scope!
lock_x_axis = False
lock_y_axis = False
def __init__(self, id):
self.id = id
# initialize angles
if not self._handle_angles:
for i, h in enumerate(Handle.EDGES):
self._handle_angles[h] = i * pi / 2.0
for i, h in enumerate(Handle.CORNERS):
self._handle_angles[h] = i * pi / 2.0 + pi / 4.0
self._handle_angles[Handle.MOVE] = 0.0
def get_rect(self):
rect = self._rect
if not rect is None and \
self.pressed:
rect = rect.offset(1.0, 1.0)
return rect
def get_radius(self):
w, h = self.get_rect().get_size()
return min(w, h) / 2.0
def get_shadow_rect(self):
rect = self.get_rect()
if rect:
rect = rect.inflate(self._shadow_size+1)
rect.w += self._shadow_offset[0]
rect.h += self._shadow_offset[1]
return rect
def get_arrow_angle(self):
return self._handle_angles[self.id]
def is_edge_handle(self):
return self.id in Handle.EDGES
def is_corner_handle(self):
return self.id in Handle.CORNERS
def update_position(self, canvas_rect):
w, h = self._size
w = min(w, canvas_rect.w / 3.0)
w = min(w, canvas_rect.h / 3.0)
h = w
self._scale = 1.0
xc, yc = canvas_rect.get_center()
if self.id is Handle.MOVE: # move handle?
d = min(canvas_rect.w - 2.0 * w, canvas_rect.h - 2.0 * h)
self._scale = 1.4
w = min(w * self._scale, d)
h = min(h * self._scale, d)
if self.id in [Handle.WEST,
Handle.NORTH_WEST,
Handle.SOUTH_WEST]:
x = canvas_rect.left()
if self.id in [Handle.NORTH,
Handle.NORTH_WEST,
Handle.NORTH_EAST]:
y = canvas_rect.top()
if self.id in [Handle.EAST,
Handle.NORTH_EAST,
Handle.SOUTH_EAST]:
x = canvas_rect.right() - w
if self.id in [Handle.SOUTH,
Handle.SOUTH_WEST,
Handle.SOUTH_EAST]:
y = canvas_rect.bottom() - h
if self.id in [Handle.MOVE, Handle.EAST, Handle.WEST]:
y = yc - h / 2.0
if self.id in [Handle.MOVE, Handle.NORTH, Handle.SOUTH]:
x = xc - w / 2.0
self._rect = Rect(x, y, w, h)
def hit_test(self, point):
rect = self.get_rect().grow(self._hit_proximity_factor)
radius = self.get_radius() * self._hit_proximity_factor
if rect and rect.is_point_within(point):
_win = self._window.get_window()
if _win:
context = _win.cairo_create()
self._build_handle_path(context, rect, radius)
return context.in_fill(*point)
return False
xc, yc = rect.get_center()
dx = xc - point[0]
dy = yc - point[1]
d = sqrt(dx*dx + dy*dy)
return d <= radius
def draw(self, context):
if self.pressed:
alpha_factor = 1.5
else:
alpha_factor = 1.0
context.new_path()
self._draw_handle_shadow(context, alpha_factor)
self._draw_handle(context, alpha_factor)
self._draw_arrows(context)
def _draw_handle(self, context, alpha_factor):
radius = self.get_radius()
line_width = radius / 15.0
alpha = self._handle_alpha * alpha_factor
if self.pressed:
context.set_source_rgba(0.78, 0.33, 0.17, alpha)
elif self.prelight:
context.set_source_rgba(0.98, 0.53, 0.37, alpha)
else:
context.set_source_rgba(0.78, 0.33, 0.17, alpha)
self._build_handle_path(context)
context.fill_preserve()
context.set_line_width(line_width)
context.stroke()
def _draw_handle_shadow(self, context, alpha_factor):
rect = self.get_rect()
context.save()
# There is a massive performance boost for groups when clipping is used.
# Integer limits are again dramatically faster (x4) then using floats.
# for 1000x draw_drop_shadow:
# with clipping: ~300ms, without: ~11000ms
context.rectangle(*self.get_shadow_rect().int())
context.clip()
context.push_group()
# draw the shadow
context.push_group_with_content(cairo.CONTENT_ALPHA)
self._build_handle_path(context)
context.set_source_rgba(0.0, 0.0, 0.0, 1.0)
context.fill()
group = context.pop_group()
drop_shadow(context, group, rect,
self._shadow_size,
self._shadow_offset,
self._shadow_alpha,
5)
# cut out the handle area, because the handle is transparent
context.save()
context.set_operator(cairo.OPERATOR_CLEAR)
context.set_source_rgba(0.0, 0.0, 0.0, 1.0)
self._build_handle_path(context)
context.fill()
context.restore()
context.pop_group_to_source()
context.paint()
context.restore()
def _draw_arrows(self, context):
radius = self.get_radius()
xc, yc = self.get_rect().get_center()
scale = radius / 2.0 / self._scale * 1.2
angle = self.get_arrow_angle()
if self.id == Handle.MOVE:
num_arrows = 4
if self.lock_x_axis:
num_arrows -= 2
angle += pi * 0.5
if self.lock_y_axis:
num_arrows -= 2
else:
num_arrows = 2
angle_step = 2.0 * pi / num_arrows
for i in range(num_arrows):
context.save()
context.translate(xc, yc)
context.rotate(angle + i * angle_step)
context.scale(scale, scale)
# arrow distance from center
if self.id is Handle.MOVE:
context.translate(0.9, 0)
else:
context.translate(0.30, 0)
self._draw_arrow(context)
context.restore()
def _draw_arrow(self, context):
context.move_to( 0.0, -0.5)
context.line_to( 0.5, 0.0)
context.line_to( 0.0, 0.5)
context.close_path()
context.set_source_rgba(1.0, 1.0, 1.0, 0.8)
context.fill_preserve()
context.set_source_rgba(0.0, 0.0, 0.0, 0.8)
context.set_line_width(0)
context.stroke()
def _build_handle_path(self, context, rect = None, radius = None):
if rect is None:
rect = self.get_rect()
if radius is None:
radius = self.get_radius()
xc, yc = rect.get_center()
corner_radius = self.corner_radius
angle = self.get_arrow_angle()
m = cairo.Matrix()
m.translate(xc, yc)
m.rotate(angle)
if self.is_edge_handle():
p0 = m.transform_point(radius, -radius)
p1 = m.transform_point(radius, radius)
context.arc(xc, yc, radius, angle + pi / 2.0, angle + pi / 2.0 + pi)
context.line_to(*p0)
context.line_to(*p1)
context.close_path()
elif self.is_corner_handle():
m.rotate(-pi / 4.0) # rotate to SOUTH_EAST
context.arc(xc, yc, radius, angle + 3 * pi / 4.0,
angle + 5 * pi / 4.0)
pt = m.transform_point(radius, -radius)
context.line_to(*pt)
if corner_radius:
# outer corner, following the rounded window corner
pt = m.transform_point(radius, radius - corner_radius)
ptc = m.transform_point(radius - corner_radius,
radius - corner_radius)
context.line_to(*pt)
context.arc(ptc[0], ptc[1], corner_radius,
angle - pi / 4.0, angle + pi / 4.0)
else:
pt = m.transform_point(radius, radius)
context.line_to(*pt)
pt = m.transform_point(-radius, radius)
context.line_to(*pt)
context.close_path()
else:
context.arc(xc, yc, radius, 0, 2.0 * pi)
def redraw(self):
rect = self.get_shadow_rect()
if rect:
self._window.queue_draw_area(*rect)
class TouchHandles(object):
""" Full set of resize and move handles """
active = False
opacity = 1.0
rect = None
def __init__(self):
self.handles = []
self._handle_pool = [TouchHandle(Handle.MOVE),
TouchHandle(Handle.NORTH_WEST),
TouchHandle(Handle.NORTH),
TouchHandle(Handle.NORTH_EAST),
TouchHandle(Handle.EAST),
TouchHandle(Handle.SOUTH_EAST),
TouchHandle(Handle.SOUTH),
TouchHandle(Handle.SOUTH_WEST),
TouchHandle(Handle.WEST)]
def set_active_handles(self, handle_ids):
self.handles = []
for handle in self._handle_pool:
if handle.id in handle_ids:
self.handles.append(handle)
def set_window(self, window):
for handle in self._handle_pool:
handle._window = window
def update_positions(self, canvas_rect):
self.rect = canvas_rect
for handle in self.handles:
handle.update_position(canvas_rect)
def draw(self, context):
if self.opacity:
clip_rect = Rect.from_extents(*context.clip_extents())
for handle in self.handles:
rect = handle.get_shadow_rect()
if rect.intersects(clip_rect):
context.save()
context.rectangle(*rect.int())
context.clip()
context.push_group()
handle.draw(context)
context.pop_group_to_source()
context.paint_with_alpha(self.opacity);
context.restore()
def redraw(self):
if self.rect:
for handle in self.handles:
handle.redraw()
def hit_test(self, point):
if self.active:
for handle in self.handles:
if handle.hit_test(point):
return handle.id
def set_prelight(self, handle_id):
for handle in self.handles:
prelight = handle.id == handle_id and not handle.pressed
if handle.prelight != prelight:
handle.prelight = prelight
handle.redraw()
def set_pressed(self, handle_id):
for handle in self.handles:
pressed = handle.id == handle_id
if handle.pressed != pressed:
handle.pressed = pressed
handle.redraw()
def set_corner_radius(self, corner_radius):
for handle in self.handles:
handle.corner_radius = corner_radius
def set_monitor_dimensions(self, size_px, size_mm):
min_monitor_mm = 50
target_size_mm = (5, 5)
min_size = TouchHandle._fallback_size
if size_mm[0] < min_monitor_mm or \
size_mm[1] < min_monitor_mm:
w = 0
h = 0
else:
w = size_px[0] / size_mm[0] * target_size_mm[0]
h = size_px[0] / size_mm[0] * target_size_mm[0]
size = max(w, min_size[0]), max(h, min_size[1])
TouchHandle._size = size
def lock_x_axis(self, lock):
""" Set to False to constraint movement in x. """
for handle in self.handles:
handle.lock_x_axis = lock
def lock_y_axis(self, lock):
""" Set to True to constraint movement in y. """
for handle in self.handles:
handle.lock_y_axis = lock
|