This repository has been archived on 2023-09-06. You can view files and clone it, but cannot push or open issues/pull-requests.
yutent
/
py-gtk-notes
Archived
1
0
Fork 0

增加显示器交互

master
yutent 2023-07-26 17:55:29 +08:00
parent f6ac50c4cf
commit 1c3595c23a
5 changed files with 93 additions and 2 deletions

9
app.js
View File

@ -38,6 +38,15 @@ bind($('.btn4'), 'click', async function () {
// native.clipboard.clear()
})
bind($('.btn5'), 'click', async function () {
native.tray()
})
bind($('.btn6'), 'click', async function () {
console.log(await native.screen.getAllDisplays())
console.log(await native.screen.getPrimaryDisplay())
})
bind($('textarea'), 'paste', async function (ev) {
let items = ev.clipboardData.items
for (let it of items) {

View File

@ -34,6 +34,8 @@
<hr>
<button class="btn3">写图片到剪切板</button>
<button class="btn4">退出</button>
<button class="btn5">tray</button>
<button class="btn6">screen</button>
<img style="width:100px;border:1px solid #09f;" src="" alt="">
<textarea></textarea>

View File

@ -185,5 +185,13 @@ Object.assign(native, {
return handler('clipboard', { action: 'clear' })
}
},
screen: {
getAllDisplays() {
return handler('monitor', { action: 'get-all' })
},
getPrimaryDisplay() {
return handler('monitor', { action: 'get-primary' })
}
},
handler
})

46
main.py
View File

@ -18,6 +18,22 @@ except (ValueError, ImportError):
AppIndicator3 = None
def get_monitor_info(monitor):
return {
"model": monitor.props.model,
"scale_factor": monitor.props.scale_factor,
"manufacturer": monitor.props.manufacturer,
"refresh_rate": monitor.props.refresh_rate,
"is_primary": monitor.is_primary(),
"geometry": {
"width": monitor.props.geometry.width,
"height": monitor.props.geometry.height,
"x": monitor.props.geometry.x,
"y": monitor.props.geometry.y,
}
}
class WebKitWindow(Gtk.Window):
def __init__(self):
@ -59,8 +75,8 @@ class WebKitWindow(Gtk.Window):
# self.webview.load_uri("http://127.0.0.1:10086/index.html")
self.webview.load_uri("https://benchmark.wkit.fun")
self.webview.load_uri("http://127.0.0.1:10086/index.html")
# self.webview.load_uri("https://benchmark.wkit.fun")
self.clipboard = Gtk.Clipboard.get(Gdk.SELECTION_CLIPBOARD)
@ -72,6 +88,8 @@ class WebKitWindow(Gtk.Window):
def create_tray(self):
if AppIndicator3 :
@ -190,6 +208,25 @@ class WebKitWindow(Gtk.Window):
self.webview.send_message_to_page(WebKit2.UserMessage.new('blabla'))
self.webview.evaluate_javascript(scripts, -1)
case 'monitor':
if params['action'] == 'get-all':
display = Gdk.Display.get_default()
monitor_num = display.get_n_monitors()
monitors = [display.get_monitor(i) for i in range(monitor_num)]
monitors = [get_monitor_info(m) for m in monitors]
scripts = 'native.$emit("' + callback + '",' + json.dumps(monitors) + ')'
self.webview.evaluate_javascript(scripts, -1)
elif params['action'] == 'get-primary':
display = Gdk.Display.get_default()
monitor = display.get_primary_monitor()
info = get_monitor_info(monitor)
scripts = 'native.$emit("' + callback + '",' + json.dumps(info) + ')'
self.webview.evaluate_javascript(scripts, -1)
case _:
if callback :
res = {"foo": 123, "bar": (11,22,33)}
@ -206,6 +243,11 @@ win = WebKitWindow()
win.connect("destroy", Gtk.main_quit)
win.show_all()
tray = win.create_tray()
Gtk.main()

30
notes/monitor.py Normal file
View File

@ -0,0 +1,30 @@
import gi
gi.require_version("Gtk", "3.0")
gi.require_version("WebKit2", "4.1")
from gi.repository import Gtk, Gdk
def get_monitor_info(monitor):
return {
"model": monitor.props.model,
"scale_factor": monitor.props.scale_factor,
"manufacturer": monitor.props.manufacturer,
"refresh_rate": monitor.props.refresh_rate,
"is_primary": monitor.is_primary(),
"geometry": {
"width": monitor.props.geometry.width,
"height": monitor.props.geometry.height,
"x": monitor.props.geometry.x,
"y": monitor.props.geometry.y,
}
}
display = Gdk.Display.get_default()
monitor_num = display.get_n_monitors()
monitors = [display.get_monitor(i) for i in range(monitor_num)]
monitors = [get_monitor_info(m) for m in monitors]