feat: 将动画启用控制权交给前端用户
后端不再主动启用动画,改为由前端通过开关控制动画状态 移除后端冗余的动画禁用逻辑,仅根据前端状态传递动画对象
This commit is contained in:
3
main.py
3
main.py
@@ -105,9 +105,8 @@ def run_egm(para: Parameter, animation=None) -> dict:
|
|||||||
avr_n_sf = 0 # 考虑电压的影响计算的跳闸率
|
avr_n_sf = 0 # 考虑电压的影响计算的跳闸率
|
||||||
ground_angels = para.ground_angels
|
ground_angels = para.ground_angels
|
||||||
# 动画对象:如果传入了 animation 则使用,否则不启用动画
|
# 动画对象:如果传入了 animation 则使用,否则不启用动画
|
||||||
|
# 注意:动画的启用由前端用户通过"启用动画"开关控制,后端不主动启用
|
||||||
animate = animation
|
animate = animation
|
||||||
if animate:
|
|
||||||
animate.enable(True) # 启用动画
|
|
||||||
# animate.show()
|
# animate.show()
|
||||||
for ground_angel in ground_angels:
|
for ground_angel in ground_angels:
|
||||||
logger.info(f"地面倾角{ground_angel/math.pi*180:.3f}°")
|
logger.info(f"地面倾角{ground_angel/math.pi*180:.3f}°")
|
||||||
|
|||||||
@@ -211,6 +211,7 @@ const animationApi = {
|
|||||||
enabled.value = enable
|
enabled.value = enable
|
||||||
onEnableChange(enable)
|
onEnableChange(enable)
|
||||||
},
|
},
|
||||||
|
isEnabled: () => enabled.value,
|
||||||
initFig,
|
initFig,
|
||||||
clear,
|
clear,
|
||||||
addRgLine,
|
addRgLine,
|
||||||
|
|||||||
@@ -537,7 +537,12 @@ const calculate = async () => {
|
|||||||
if (window.pywebview) {
|
if (window.pywebview) {
|
||||||
// 后台线程启动计算,实时日志通过 addLogFromBackend 推送
|
// 后台线程启动计算,实时日志通过 addLogFromBackend 推送
|
||||||
// 结果通过 receiveResult 回调接收
|
// 结果通过 receiveResult 回调接收
|
||||||
await window.pywebview.api.calculate(params)
|
// 传递动画启用状态
|
||||||
|
const paramsWithAnimation = {
|
||||||
|
...params,
|
||||||
|
animation_enabled: animationRef.value?.isEnabled() ?? false
|
||||||
|
}
|
||||||
|
await window.pywebview.api.calculate(paramsWithAnimation)
|
||||||
// 不在这里设置 calculating = false,等待 receiveResult 回调
|
// 不在这里设置 calculating = false,等待 receiveResult 回调
|
||||||
} else {
|
} else {
|
||||||
// 开发模式下的模拟
|
// 开发模式下的模拟
|
||||||
|
|||||||
@@ -27,11 +27,13 @@ class WebAnimation:
|
|||||||
"""
|
"""
|
||||||
Web 动画类,将 Python 端的 Animation 调用映射到前端 JavaScript
|
Web 动画类,将 Python 端的 Animation 调用映射到前端 JavaScript
|
||||||
对应 Animation.vue 的功能
|
对应 Animation.vue 的功能
|
||||||
|
|
||||||
|
注意:动画的启用/禁用由前端用户通过"启用动画"开关控制,
|
||||||
|
后端只负责发送绘制指令,前端根据 enabled 状态决定是否执行
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, window=None):
|
def __init__(self, window=None):
|
||||||
self._window = window
|
self._window = window
|
||||||
self._disable = True # 默认禁用
|
|
||||||
|
|
||||||
def set_window(self, window):
|
def set_window(self, window):
|
||||||
"""设置窗口对象"""
|
"""设置窗口对象"""
|
||||||
@@ -39,17 +41,16 @@ class WebAnimation:
|
|||||||
|
|
||||||
def enable(self, enabled: bool):
|
def enable(self, enabled: bool):
|
||||||
"""
|
"""
|
||||||
启用/禁用动画
|
启用/禁用动画(由前端用户控制)
|
||||||
对应 animation.py 的 enable 方法和 Animation.vue 的 enable 方法
|
此方法保留以兼容接口,但实际启用状态由前端控制
|
||||||
"""
|
"""
|
||||||
self._disable = not enabled
|
|
||||||
if self._window:
|
if self._window:
|
||||||
js_code = f'if(window.animationApi){{window.animationApi.enable({str(enabled).lower()})}}'
|
js_code = f'if(window.animationApi){{window.animationApi.enable({str(enabled).lower()})}}'
|
||||||
self._window.evaluate_js(js_code)
|
self._window.evaluate_js(js_code)
|
||||||
|
|
||||||
def init_fig(self):
|
def init_fig(self):
|
||||||
"""初始化画布"""
|
"""初始化画布"""
|
||||||
if self._disable or not self._window:
|
if not self._window:
|
||||||
return
|
return
|
||||||
js_code = 'if(window.animationApi){window.animationApi.initFig()}'
|
js_code = 'if(window.animationApi){window.animationApi.initFig()}'
|
||||||
self._window.evaluate_js(js_code)
|
self._window.evaluate_js(js_code)
|
||||||
@@ -59,7 +60,7 @@ class WebAnimation:
|
|||||||
添加地线保护弧(RS 圆)
|
添加地线保护弧(RS 圆)
|
||||||
对应 animation.py 的 add_rs 方法
|
对应 animation.py 的 add_rs 方法
|
||||||
"""
|
"""
|
||||||
if self._disable or not self._window:
|
if not self._window:
|
||||||
return
|
return
|
||||||
js_code = f'if(window.animationApi){{window.animationApi.addRs({rs}, {rs_x}, {rs_y})}}'
|
js_code = f'if(window.animationApi){{window.animationApi.addRs({rs}, {rs_x}, {rs_y})}}'
|
||||||
self._window.evaluate_js(js_code)
|
self._window.evaluate_js(js_code)
|
||||||
@@ -69,7 +70,7 @@ class WebAnimation:
|
|||||||
添加导线暴露弧(RC 圆)
|
添加导线暴露弧(RC 圆)
|
||||||
对应 animation.py 的 add_rc 方法
|
对应 animation.py 的 add_rc 方法
|
||||||
"""
|
"""
|
||||||
if self._disable or not self._window:
|
if not self._window:
|
||||||
return
|
return
|
||||||
js_code = f'if(window.animationApi){{window.animationApi.addRc({rc}, {rc_x}, {rc_y})}}'
|
js_code = f'if(window.animationApi){{window.animationApi.addRc({rc}, {rc_x}, {rc_y})}}'
|
||||||
self._window.evaluate_js(js_code)
|
self._window.evaluate_js(js_code)
|
||||||
@@ -82,7 +83,7 @@ class WebAnimation:
|
|||||||
Args:
|
Args:
|
||||||
line_func: 一个函数,接收 x 返回 y
|
line_func: 一个函数,接收 x 返回 y
|
||||||
"""
|
"""
|
||||||
if self._disable or not self._window:
|
if not self._window:
|
||||||
return
|
return
|
||||||
# 生成线上的点,传递给前端
|
# 生成线上的点,传递给前端
|
||||||
# 由于无法直接传递函数,我们预先计算一些点
|
# 由于无法直接传递函数,我们预先计算一些点
|
||||||
@@ -111,7 +112,7 @@ class WebAnimation:
|
|||||||
添加暴露弧区域(两条红线)
|
添加暴露弧区域(两条红线)
|
||||||
对应 animation.py 的 add_expose_area 方法
|
对应 animation.py 的 add_expose_area 方法
|
||||||
"""
|
"""
|
||||||
if self._disable or not self._window:
|
if not self._window:
|
||||||
return
|
return
|
||||||
js_code = f'''if(window.animationApi){{
|
js_code = f'''if(window.animationApi){{
|
||||||
window.animationApi.addExposeArea(
|
window.animationApi.addExposeArea(
|
||||||
@@ -124,7 +125,7 @@ class WebAnimation:
|
|||||||
|
|
||||||
def clear(self):
|
def clear(self):
|
||||||
"""清除画布"""
|
"""清除画布"""
|
||||||
if self._disable or not self._window:
|
if not self._window:
|
||||||
return
|
return
|
||||||
js_code = 'if(window.animationApi){window.animationApi.clear()}'
|
js_code = 'if(window.animationApi){window.animationApi.clear()}'
|
||||||
self._window.evaluate_js(js_code)
|
self._window.evaluate_js(js_code)
|
||||||
@@ -134,7 +135,7 @@ class WebAnimation:
|
|||||||
暂停并刷新
|
暂停并刷新
|
||||||
对应 animation.py 的 pause 方法
|
对应 animation.py 的 pause 方法
|
||||||
"""
|
"""
|
||||||
if self._disable or not self._window:
|
if not self._window:
|
||||||
return
|
return
|
||||||
js_code = 'if(window.animationApi){window.animationApi.pause()}'
|
js_code = 'if(window.animationApi){window.animationApi.pause()}'
|
||||||
self._window.evaluate_js(js_code)
|
self._window.evaluate_js(js_code)
|
||||||
@@ -341,8 +342,12 @@ class EGMWebApp:
|
|||||||
|
|
||||||
logger.info("开始执行 EGM 计算...")
|
logger.info("开始执行 EGM 计算...")
|
||||||
|
|
||||||
# 调用 main.py 的核心计算函数,传递 animation 对象
|
# 根据前端动画启用状态决定是否传递 animation 对象
|
||||||
result = run_egm(para, self.animation)
|
animation_enabled = params.get('animation_enabled', False)
|
||||||
|
animation_obj = self.animation if animation_enabled else None
|
||||||
|
|
||||||
|
# 调用 main.py 的核心计算函数
|
||||||
|
result = run_egm(para, animation_obj)
|
||||||
|
|
||||||
self.add_log("info", "EGM 计算完成")
|
self.add_log("info", "EGM 计算完成")
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user