File: uprogressDialog.py

package info (click to toggle)
far2l 2.7.0~beta%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 44,304 kB
  • sloc: cpp: 263,566; ansic: 53,886; python: 7,048; sh: 1,516; perl: 410; javascript: 279; xml: 145; makefile: 31
file content (92 lines) | stat: -rw-r--r-- 2,340 bytes parent folder | download | duplicates (5)
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)