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
|
import time
import logging
import threading
from far2l.plugin import PluginBase
from far2l.fardialogbuilder import (
Spacer,
TEXT,
BUTTON,
HLine,
HSizer,
VSizer,
DialogBuilder,
)
log = logging.getLogger(__name__)
class ProgressThread(threading.Thread):
def __init__(self):
super().__init__()
self.cond = threading.Lock()
self.daemon = True
self.sleep = 0.1
self.vmin = 0
self.vmax = 100
self.done = False
def cancel(self):
if not self.done:
self.vmin = -self.vmin
def run(self):
while self.vmin >= 0 and self.vmin < self.vmax:
self.cond.acquire(True)
self.vmin += 1
self.cond.release()
time.sleep(self.sleep)
self.done = True
class Plugin(PluginBase):
label = "Python Progress Dialog"
openFrom = ["PLUGINSMENU"]
def OnIdle(self, t, dlg):
if not t.cond.acquire(False):
return
self._OnIdle(t, dlg)
t.cond.release()
def _OnIdle(self, t, dlg):
n = t.vmin * 20 // t.vmax
fprog = (('='*n)+' '*20)[:20]
dlg.SetText(dlg.ID_fprog, '[{}]'.format(fprog))
def OpenPlugin(self, OpenFrom):
t = ProgressThread()
@self.ffi.callback("FARWINDOWPROC")
def DialogProc(hDlg, Msg, Param1, Param2):
if Msg == self.ffic.DN_INITDIALOG:
dlg.SetText(dlg.ID_fprog, "")
# start thread
t.start()
elif Msg == self.ffic.DN_ENTERIDLE:
if t.done:
dlg.Close(0)
else:
self.OnIdle(t, dlg)
return self.info.DefDlgProc(hDlg, Msg, Param1, Param2)
b = DialogBuilder(
self,
DialogProc,
"Python Progress Demo",
"helptopic",
self.ffic.FDLG_REGULARIDLE,
VSizer(
TEXT("fprog", " "*(2+20)),
HLine(),
BUTTON("vcancel", "Cancel", flags=self.ffic.DIF_CENTERGROUP),
),
)
dlg = b.build(-1, -1)
res = self.info.DialogRun(dlg.hDlg)
t.cancel()
v = t.vmin
del t
log.debug('t.vmin={} res={}'.format(v, res))
self.info.DialogFree(dlg.hDlg)
|