-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
transwin.el
163 lines (128 loc) · 5.23 KB
/
transwin.el
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
;;; transwin.el --- Make window/frame transparent -*- lexical-binding: t; -*-
;; Copyright (C) 2020-2024 Shen, Jen-Chieh
;; Created date 2020-06-25 01:42:34
;; Author: Shen, Jen-Chieh <[email protected]>
;; URL: https://github.com/jcs-elpa/transwin
;; Version: 0.1.4
;; Package-Requires: ((emacs "24.3"))
;; Keywords: frames window transparent
;; This file is NOT part of GNU Emacs.
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;;
;; Make window/frame transparent.
;;
;;; Code:
(defgroup transwin nil
"Make window/frame transparent."
:prefix "transwin-"
:group 'tool
:link '(url-link :tag "Repository" "https://github.com/jcs-elpa/transwin"))
(defcustom transwin-delta-alpha 5
"Delta value increament/decreament transparency value."
:type 'integer
:group 'transwin)
(defcustom transwin-parameter-alpha 'alpha
"Frame parameter symbol."
:type 'symbol
:group 'transwin)
(defvar transwin--current-alpha 100
"Current alpha level.")
(defvar transwin--record-toggle-frame-transparency 85
"Record toggle frame transparency.")
;;; Util
(defun transwin--to-reverse (val)
"Reverse value VAL."
(- 0 val))
(defun transwin--to-positive (val)
"Convert VAL to positive value."
(when (and val (< val 0))
(setq val (transwin--to-reverse val)))
val)
(defun transwin--to-negative (val)
"Convert VAL to negative value."
(when (and val (> val 0))
(setq val (transwin--to-reverse val)))
val)
(defun transwin--clamp-integer (val min max)
"Make sure the VAL is between MIN and MAX."
(cond ((<= val min) min)
((>= val max) max)
(t val)))
(defun transwin--log (fmt &rest args)
"Log message like function `message' with same argument FMT and ARGS."
(let (message-log-max) (apply 'message fmt args)))
;;; Core
(defun transwin--set-transparency (alpha-level)
"Set the frame transparency by ALPHA-LEVEL."
(setq alpha-level (max alpha-level (if (floatp frame-alpha-lower-limit)
(* 100 frame-alpha-lower-limit)
frame-alpha-lower-limit))
alpha-level (round alpha-level))
(set-frame-parameter nil transwin-parameter-alpha alpha-level)
(transwin--log "[INFO] Frame alpha level is %d" (frame-parameter nil transwin-parameter-alpha))
(setq transwin--current-alpha alpha-level)
(unless (= alpha-level 100)
(setq transwin--record-toggle-frame-transparency alpha-level)))
(defun transwin--delta-frame-transparent (del-trans)
"Delta change the frame transparency by a certain percentage, DEL-TRANS."
(let ((alpha (or (frame-parameter nil transwin-parameter-alpha) 100))
(current-transparency transwin-delta-alpha))
(setq current-transparency
(cond ((numberp alpha) alpha)
((numberp (cdr alpha)) (cdr alpha))
;; Also handle undocumented (<active> <inactive>) form.
((numberp (cadr alpha)) (cadr alpha))))
(setq current-transparency (+ current-transparency del-trans))
(setq current-transparency (transwin--clamp-integer current-transparency 5 100))
;; Apply the value to frame.
(transwin--set-transparency current-transparency)))
;;;###autoload
(defun transwin-inc (&optional del-trans)
"Increment the frame transparency by a certain percentage, DEL-TRANS."
(interactive)
(transwin--delta-frame-transparent (transwin--to-positive (or del-trans transwin-delta-alpha))))
;;;###autoload
(defun transwin-dec (&optional del-trans)
"Decrement the frame transparency by a certain percentage, DEL-TRANS."
(interactive)
(transwin--delta-frame-transparent (transwin--to-negative (or del-trans transwin-delta-alpha))))
;;;###autoload
(defun transwin-ask (alpha-level)
"Set the frame transparency by ALPHA-LEVEL."
(interactive "p")
(let ((alpha-level (if (< alpha-level 2)
(read-number "Opacity percentage: " transwin--record-toggle-frame-transparency)
alpha-level)))
(transwin--set-transparency alpha-level)))
;;;###autoload
(defun transwin-toggle ()
"Toggle frame's transparency between `recorded'% and 100%."
(interactive)
(if (= transwin--current-alpha 100)
(transwin--set-transparency transwin--record-toggle-frame-transparency)
(transwin--set-transparency 100)))
;;; Obsolete
(define-obsolete-function-alias
'transwin-increment-frame-transparent
'transwin-inc "0.1.4")
(define-obsolete-function-alias
'transwin-decrement-frame-transparent
'transwin-dec "0.1.4")
(define-obsolete-function-alias
'transwin-ask-set-transparency
'transwin-ask "0.1.4")
(define-obsolete-function-alias
'transwin-toggle-transparent-frame
'transwin-toggle "0.1.4")
(provide 'transwin)
;;; transwin.el ends here