110 lines
3.5 KiB
Python
110 lines
3.5 KiB
Python
from PIL import Image, ImageDraw
|
|
|
|
|
|
class NotepadApp:
|
|
kind = 'notepad'
|
|
title = 'Блокнот'
|
|
icon = 'N'
|
|
cmd = 'notepad'
|
|
|
|
ROWS = 5 # Не перекрывает панель задач
|
|
COLS = 20
|
|
|
|
def __init__(self):
|
|
self.lines = [""]
|
|
self.cx = 0
|
|
self.cy = 0
|
|
self.sx = 0
|
|
self.sy = 0
|
|
|
|
def render(self, fonts, bg, now):
|
|
img = Image.new('1', (128, 64), 1) # Белый фон
|
|
d = ImageDraw.Draw(img)
|
|
for i in range(self.ROWS):
|
|
idx = self.sy + i
|
|
if idx < len(self.lines):
|
|
line = self.lines[idx]
|
|
# Отступ убран, теперь как в терминале: y = i * 10
|
|
d.text((2, i * 10),
|
|
line[self.sx:self.sx + self.COLS],
|
|
font=fonts['term'], fill=0)
|
|
|
|
# Позиция курсора тоже без отступа +2
|
|
tw = d.textlength(self.lines[self.cy][self.sx:self.cx], font=fonts['term'])
|
|
cur_x = 2 + int(tw)
|
|
cur_y = (self.cy - self.sy) * 10
|
|
d.rectangle((cur_x, cur_y, cur_x + 1, cur_y + 8), fill=0)
|
|
return img
|
|
|
|
def handle_key(self, code, mods, system):
|
|
if code in (28, 96): self._new_line(); return True
|
|
if code == 14: self._backspace(); return True
|
|
if code == 103: self._move_up(); return True
|
|
if code == 108: self._move_down(); return True
|
|
if code == 105: self._move_left(); return True
|
|
if code == 106: self._move_right(); return True
|
|
ch = system._char_for(code, mods.get('shift', False))
|
|
if ch and len(ch) == 1:
|
|
self._insert(ch); return True
|
|
return False
|
|
|
|
def _insert(self, ch):
|
|
line = self.lines[self.cy]
|
|
self.lines[self.cy] = line[:self.cx] + ch + line[self.cx:]
|
|
self.cx += 1
|
|
self._update_scroll()
|
|
|
|
def _new_line(self):
|
|
line = self.lines[self.cy]
|
|
self.lines[self.cy] = line[:self.cx]
|
|
self.lines.insert(self.cy + 1, line[self.cx:])
|
|
self.cy += 1
|
|
self.cx = 0
|
|
self._update_scroll()
|
|
|
|
def _backspace(self):
|
|
line = self.lines[self.cy]
|
|
if self.cx > 0:
|
|
self.lines[self.cy] = line[:self.cx - 1] + line[self.cx:]
|
|
self.cx -= 1
|
|
elif self.cy > 0:
|
|
self.cx = len(self.lines[self.cy - 1])
|
|
self.lines[self.cy - 1] += line
|
|
del self.lines[self.cy]
|
|
self.cy -= 1
|
|
self._update_scroll()
|
|
|
|
def _move_up(self):
|
|
if self.cy > 0:
|
|
self.cy -= 1
|
|
self.cx = min(self.cx, len(self.lines[self.cy]))
|
|
self._update_scroll()
|
|
|
|
def _move_down(self):
|
|
if self.cy < len(self.lines) - 1:
|
|
self.cy += 1
|
|
self.cx = min(self.cx, len(self.lines[self.cy]))
|
|
self._update_scroll()
|
|
|
|
def _move_left(self):
|
|
if self.cx > 0:
|
|
self.cx -= 1
|
|
elif self.cy > 0:
|
|
self.cy -= 1
|
|
self.cx = len(self.lines[self.cy])
|
|
self._update_scroll()
|
|
|
|
def _move_right(self):
|
|
if self.cx < len(self.lines[self.cy]):
|
|
self.cx += 1
|
|
elif self.cy < len(self.lines) - 1:
|
|
self.cy += 1
|
|
self.cx = 0
|
|
self._update_scroll()
|
|
|
|
def _update_scroll(self):
|
|
if self.cy < self.sy: self.sy = self.cy
|
|
elif self.cy >= self.sy + self.ROWS: self.sy = self.cy - self.ROWS + 1
|
|
if self.cx < self.sx: self.sx = self.cx
|
|
elif self.cx >= self.sx + self.COLS: self.sx = self.cx - self.COLS + 1
|