tao/platform_impl/linux/
monitor.rs

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
// Copyright 2014-2021 The winit contributors
// Copyright 2021-2023 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0

use gtk::gdk::{self, prelude::MonitorExt, Display};

use crate::{
  dpi::{LogicalPosition, LogicalSize, PhysicalPosition, PhysicalSize},
  monitor::{MonitorHandle as RootMonitorHandle, VideoMode as RootVideoMode},
};

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct MonitorHandle {
  pub(crate) monitor: gdk::Monitor,
}

impl MonitorHandle {
  pub fn new(display: &gdk::Display, number: i32) -> Self {
    let monitor = display.monitor(number).unwrap();
    Self { monitor }
  }

  #[inline]
  pub fn name(&self) -> Option<String> {
    self.monitor.model().map(|s| s.as_str().to_string())
  }

  #[inline]
  pub fn size(&self) -> PhysicalSize<u32> {
    let rect = self.monitor.geometry();
    LogicalSize {
      width: rect.width() as u32,
      height: rect.height() as u32,
    }
    .to_physical(self.scale_factor())
  }

  #[inline]
  pub fn position(&self) -> PhysicalPosition<i32> {
    let rect = self.monitor.geometry();
    LogicalPosition {
      x: rect.x(),
      y: rect.y(),
    }
    .to_physical(self.scale_factor())
  }

  #[inline]
  pub fn scale_factor(&self) -> f64 {
    self.monitor.scale_factor() as f64
  }

  #[inline]
  pub fn video_modes(&self) -> Box<dyn Iterator<Item = RootVideoMode>> {
    Box::new(Vec::new().into_iter())
  }
}

unsafe impl Send for MonitorHandle {}
unsafe impl Sync for MonitorHandle {}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct VideoMode;

impl VideoMode {
  #[inline]
  pub fn size(&self) -> PhysicalSize<u32> {
    panic!("VideoMode is unsupported on Linux.")
  }

  #[inline]
  pub fn bit_depth(&self) -> u16 {
    panic!("VideoMode is unsupported on Linux.")
  }

  #[inline]
  pub fn refresh_rate(&self) -> u16 {
    panic!("VideoMode is unsupported on Linux.")
  }

  #[inline]
  pub fn monitor(&self) -> RootMonitorHandle {
    panic!("VideoMode is unsupported on Linux.")
  }
}

pub fn from_point(display: &Display, x: f64, y: f64) -> Option<MonitorHandle> {
  if let Some(monitor) = display.monitor_at_point(x as i32, y as i32) {
    (0..display.n_monitors())
      .map(|i| (i, display.monitor(i).unwrap()))
      .find(|cur| cur.1.geometry() == monitor.geometry())
      .map(|x| MonitorHandle::new(display, x.0))
  } else {
    None
  }
}