1
0
Fork 0
tinmop/src/main-window.lisp

87 lines
3.1 KiB
Common Lisp

;; tinmop: a multiprotocol client
;; Copyright © cage
;; 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 <http://www.gnu.org/licenses/>.
(in-package :main-window)
(defclass main-window (wrapper-window)
((focused-window
:initform nil
:initarg :focused-window
:accessor focused-window
:documentation "The window with the focus, only a window can get
the focus at the same time"))
(:documentation "the main window AKA the screen"))
(defmethod refresh-config :after ((object main-window))
(refresh-config-colors object swconf:+key-main-window+))
(defmethod calculate ((object main-window) dt)
(do-children (child object)
(calculate child dt)))
(defmethod draw ((object main-window))
(do-children (child object)
(draw child)))
(defgeneric focused-keybindings (object))
(defmethod focused-keybindings ((object main-window))
"Return the keymap of the window with focus"
(with-accessors ((focused-window focused-window)) object
(when focused-window
(when-let ((keybindings (keybindings focused-window)))
keybindings))))
(defun init ()
"Initialize the screen"
(let ((screen (make-screen)))
(setf *main-window*
(make-instance 'main-window
:keybindings keybindings:*global-keymap*
:key-config swconf:+key-main-window+
:croatoan-window screen))
(refresh-config *main-window*)
*main-window*))
(defun parse-subwin-size (size-as-string main-window-size)
"Parse a window size, size is a fraction of the main window size"
(let* ((raw (num:safe-parse-number size-as-string
:fix-fn (lambda (e)
(declare (ignore e))
main-window-size))))
(cond
((= raw 1)
main-window-size)
((< raw 0)
(let ((fraction (abs (clamp raw -1.0 0.0))))
(truncate (- main-window-size
(* fraction main-window-size)))))
((integerp raw)
raw)
(t
(truncate (* (clamp raw 0.0 1.0)
main-window-size))))))
(defun parse-subwin-w (w-as-string)
"Parse a window width, `w-as-string' a fraction of the main window width"
#+sbcl (declare (sb-ext:muffle-conditions sb-ext:compiler-note))
(parse-subwin-size w-as-string (win-width *main-window*)))
(defun parse-subwin-h (h-as-string)
"Parse a window height, `h-as-string' a fraction of the main window height"
#+sbcl (declare (sb-ext:muffle-conditions sb-ext:compiler-note))
(parse-subwin-size h-as-string (win-height *main-window*)))