mirror of https://github.com/anoshenko/rui.git
Added StartTimer and StopTimer to Session
This commit is contained in:
parent
6c49f37f68
commit
befb2a7484
|
@ -1,3 +1,12 @@
|
||||||
|
# v0.14.0
|
||||||
|
* Added the ability to work without creating a WebSocket. Added NoSocket property of AppParams.
|
||||||
|
* Added the ability to run a timer on the client side. Added StartTimer and StopTimer methods to Session interface.
|
||||||
|
* Bug fixing
|
||||||
|
|
||||||
|
# v0.13.x
|
||||||
|
* Added NewHandler function
|
||||||
|
* Bug fixing
|
||||||
|
|
||||||
# v0.13.0
|
# v0.13.0
|
||||||
|
|
||||||
* Added SetHotKey function to Session interface
|
* Added SetHotKey function to Session interface
|
||||||
|
|
23
app_post.js
23
app_post.js
|
@ -1,16 +1,15 @@
|
||||||
|
|
||||||
function sendMessage(message) {
|
async function sendMessage(message) {
|
||||||
let xhr = new XMLHttpRequest();
|
const response = await fetch('/', {
|
||||||
xhr.open('POST', '/', true);
|
method : 'POST',
|
||||||
xhr.onreadystatechange = function() {
|
body : message,
|
||||||
const script = this.responseText
|
"Content-Type" : "text/plain",
|
||||||
if (script != "") {
|
});
|
||||||
window.eval(script)
|
|
||||||
//sendMessage("nop{session=" + sessionID +"}")
|
const text = await response.text();
|
||||||
}
|
if (text != "") {
|
||||||
|
window.eval(text)
|
||||||
}
|
}
|
||||||
xhr.send(message);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
window.onload = function() {
|
window.onload = function() {
|
||||||
|
|
|
@ -1861,6 +1861,28 @@ function imageError(element, event) {
|
||||||
sendMessage(message);
|
sendMessage(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let timers = new Map();
|
||||||
|
|
||||||
|
function startTimer(ms, timerID) {
|
||||||
|
let data = {
|
||||||
|
id: setInterval(timerFunc, ms, timerID),
|
||||||
|
ms: ms,
|
||||||
|
};
|
||||||
|
timers.set(timerID, data);
|
||||||
|
}
|
||||||
|
|
||||||
|
function timerFunc(timerID) {
|
||||||
|
sendMessage("timer{session=" + sessionID + ",timerID=" + timerID + "}");
|
||||||
|
}
|
||||||
|
|
||||||
|
function stopTimer(timerID) {
|
||||||
|
let timer = timers.get(timerID);
|
||||||
|
if (timer) {
|
||||||
|
clearInterval(timer.id);
|
||||||
|
timers.delete(timerID);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function canvasTextMetrics(answerID, elementId, font, text) {
|
function canvasTextMetrics(answerID, elementId, font, text) {
|
||||||
let w = 0;
|
let w = 0;
|
||||||
let ascent = 0;
|
let ascent = 0;
|
||||||
|
|
46
session.go
46
session.go
|
@ -110,6 +110,14 @@ type Session interface {
|
||||||
// Invoke SetHotKey(..., ..., nil) for remove hotkey function.
|
// Invoke SetHotKey(..., ..., nil) for remove hotkey function.
|
||||||
SetHotKey(keyCode KeyCode, controlKeys ControlKeyMask, fn func(Session))
|
SetHotKey(keyCode KeyCode, controlKeys ControlKeyMask, fn func(Session))
|
||||||
|
|
||||||
|
// StartTimer starts a timer on the client side.
|
||||||
|
// The first argument specifies the timer period in milliseconds.
|
||||||
|
// The second argument specifies a function that will be called on each timer event.
|
||||||
|
// The result is the id of the timer, which is used to stop the timer
|
||||||
|
StartTimer(ms int, timerFunc func(Session)) int
|
||||||
|
// StopTimer the timer with the given id
|
||||||
|
StopTimer(timerID int)
|
||||||
|
|
||||||
getCurrentTheme() Theme
|
getCurrentTheme() Theme
|
||||||
registerAnimation(props []AnimatedProperty) string
|
registerAnimation(props []AnimatedProperty) string
|
||||||
|
|
||||||
|
@ -197,6 +205,8 @@ type sessionData struct {
|
||||||
updateScripts map[string]*strings.Builder
|
updateScripts map[string]*strings.Builder
|
||||||
clientStorage map[string]string
|
clientStorage map[string]string
|
||||||
hotkeys map[string]func(Session)
|
hotkeys map[string]func(Session)
|
||||||
|
timers map[int]func(Session)
|
||||||
|
nextTimerID int
|
||||||
}
|
}
|
||||||
|
|
||||||
func newSession(app Application, id int, customTheme string, params DataObject) Session {
|
func newSession(app Application, id int, customTheme string, params DataObject) Session {
|
||||||
|
@ -215,6 +225,8 @@ func newSession(app Application, id int, customTheme string, params DataObject)
|
||||||
session.updateScripts = map[string]*strings.Builder{}
|
session.updateScripts = map[string]*strings.Builder{}
|
||||||
session.clientStorage = map[string]string{}
|
session.clientStorage = map[string]string{}
|
||||||
session.hotkeys = map[string]func(Session){}
|
session.hotkeys = map[string]func(Session){}
|
||||||
|
session.timers = map[int]func(Session){}
|
||||||
|
session.nextTimerID = 1
|
||||||
|
|
||||||
if customTheme != "" {
|
if customTheme != "" {
|
||||||
if theme, ok := CreateThemeFromText(customTheme); ok {
|
if theme, ok := CreateThemeFromText(customTheme); ok {
|
||||||
|
@ -664,6 +676,22 @@ func (session *sessionData) handleEvent(command string, data DataObject) {
|
||||||
case "session-resume":
|
case "session-resume":
|
||||||
session.onResume()
|
session.onResume()
|
||||||
|
|
||||||
|
case "timer":
|
||||||
|
if text, ok := data.PropertyValue("timerID"); ok {
|
||||||
|
timerID, err := strconv.Atoi(text)
|
||||||
|
if err == nil {
|
||||||
|
if fn, ok := session.timers[timerID]; ok {
|
||||||
|
fn(session)
|
||||||
|
} else {
|
||||||
|
ErrorLog(`Timer (id = ` + text + `) not exists`)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
ErrorLog(err.Error())
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
ErrorLog(`"timerID" property not found`)
|
||||||
|
}
|
||||||
|
|
||||||
case "root-size":
|
case "root-size":
|
||||||
session.handleRootSize(data)
|
session.handleRootSize(data)
|
||||||
|
|
||||||
|
@ -797,3 +825,21 @@ func (session *sessionData) RemoveAllClientItems() {
|
||||||
func (session *sessionData) addToEventsQueue(data DataObject) {
|
func (session *sessionData) addToEventsQueue(data DataObject) {
|
||||||
session.events <- data
|
session.events <- data
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (session *sessionData) StartTimer(ms int, timerFunc func(Session)) int {
|
||||||
|
timerID := 0
|
||||||
|
if session.bridge != nil {
|
||||||
|
timerID = session.nextTimerID
|
||||||
|
session.nextTimerID++
|
||||||
|
session.timers[timerID] = timerFunc
|
||||||
|
session.bridge.callFunc("startTimer", ms, timerID)
|
||||||
|
}
|
||||||
|
return timerID
|
||||||
|
}
|
||||||
|
|
||||||
|
func (session *sessionData) StopTimer(timerID int) {
|
||||||
|
if session.bridge != nil {
|
||||||
|
session.bridge.callFunc("stopTimer", timerID)
|
||||||
|
delete(session.timers, timerID)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
12
webBridge.go
12
webBridge.go
|
@ -397,7 +397,7 @@ func (bridge *webBridge) canvasFinish() {
|
||||||
bridge.writeMessage(bridge.canvasBuffer.String())
|
bridge.writeMessage(bridge.canvasBuffer.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
func (bridge *webBridge) removeValue(funcName, htmlID string, args ...string) (DataObject, bool) {
|
func (bridge *webBridge) remoteValue(funcName string, args ...any) (DataObject, bool) {
|
||||||
bridge.answerMutex.Lock()
|
bridge.answerMutex.Lock()
|
||||||
answerID := bridge.answerID
|
answerID := bridge.answerID
|
||||||
bridge.answerID++
|
bridge.answerID++
|
||||||
|
@ -406,11 +406,7 @@ func (bridge *webBridge) removeValue(funcName, htmlID string, args ...string) (D
|
||||||
answer := make(chan DataObject)
|
answer := make(chan DataObject)
|
||||||
bridge.answer[answerID] = answer
|
bridge.answer[answerID] = answer
|
||||||
|
|
||||||
funcArgs := []any{answerID, htmlID}
|
funcArgs := append([]any{answerID}, args...)
|
||||||
for _, arg := range args {
|
|
||||||
funcArgs = append(funcArgs, arg)
|
|
||||||
}
|
|
||||||
|
|
||||||
var result DataObject = nil
|
var result DataObject = nil
|
||||||
ok := bridge.callFuncImmediately(funcName, funcArgs...)
|
ok := bridge.callFuncImmediately(funcName, funcArgs...)
|
||||||
if ok {
|
if ok {
|
||||||
|
@ -424,14 +420,14 @@ func (bridge *webBridge) removeValue(funcName, htmlID string, args ...string) (D
|
||||||
|
|
||||||
func (bridge *webBridge) canvasTextMetrics(htmlID, font, text string) TextMetrics {
|
func (bridge *webBridge) canvasTextMetrics(htmlID, font, text string) TextMetrics {
|
||||||
result := TextMetrics{}
|
result := TextMetrics{}
|
||||||
if data, ok := bridge.removeValue("canvasTextMetrics", htmlID, font, text); ok {
|
if data, ok := bridge.remoteValue("canvasTextMetrics", htmlID, font, text); ok {
|
||||||
result.Width = dataFloatProperty(data, "width")
|
result.Width = dataFloatProperty(data, "width")
|
||||||
}
|
}
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
func (bridge *webBridge) htmlPropertyValue(htmlID, name string) string {
|
func (bridge *webBridge) htmlPropertyValue(htmlID, name string) string {
|
||||||
if data, ok := bridge.removeValue("getPropertyValue", htmlID, name); ok {
|
if data, ok := bridge.remoteValue("getPropertyValue", htmlID, name); ok {
|
||||||
if value, ok := data.PropertyValue("value"); ok {
|
if value, ok := data.PropertyValue("value"); ok {
|
||||||
return value
|
return value
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue