Update xiaoshi-device-todo-button.js

This commit is contained in:
xiaoshi
2026-01-01 15:45:11 +08:00
committed by GitHub
parent b5dfe6ac0a
commit bf6c7d9a1a

View File

@@ -1107,7 +1107,6 @@ class XiaoshiTodoButton extends LitElement {
super(); super();
this._todoData = []; this._todoData = [];
this._loading = false; this._loading = false;
this._dataLoaded = false; //button新元素
this._refreshInterval = null; this._refreshInterval = null;
this.theme = 'on'; this.theme = 'on';
this._editingItem = null; this._editingItem = null;
@@ -1124,7 +1123,7 @@ class XiaoshiTodoButton extends LitElement {
// 设置主题属性 // 设置主题属性
this.setAttribute('theme', this._evaluateTheme()); this.setAttribute('theme', this._evaluateTheme());
// 每300秒刷新一次数据减少频繁刷新 // 每300秒刷新一次数据减少频繁刷新
this._refreshInterval = setInterval(() => { this._refreshInterval = setInterval(() => {
this._loadTodoData(); this._loadTodoData();
@@ -1266,11 +1265,9 @@ class XiaoshiTodoButton extends LitElement {
} }
this._todoData = todoData; this._todoData = todoData;
this._dataLoaded = true; //button新元素
} catch (error) { } catch (error) {
console.error('加载待办事项数据失败:', error); console.error('加载待办事项数据失败:', error);
this._todoData = []; this._todoData = [];
this._dataLoaded = true; //button新元素
} }
this._loading = false; this._loading = false;
@@ -1633,7 +1630,7 @@ class XiaoshiTodoButton extends LitElement {
const buttonBgColor = transparentBg ? 'transparent' : theme === 'on' ? 'rgb(255, 255, 255, 0.6)' : 'rgb(83, 83, 83, 0.6)'; const buttonBgColor = transparentBg ? 'transparent' : theme === 'on' ? 'rgb(255, 255, 255, 0.6)' : 'rgb(83, 83, 83, 0.6)';
// 检查是否需要自动隐藏只有数据加载完成且数量为0时才考虑隐藏 // 检查是否需要自动隐藏只有数据加载完成且数量为0时才考虑隐藏
const shouldAutoHide = this._dataLoaded && autoHide && totalIncompleteCount === 0; const shouldAutoHide = autoHide && totalIncompleteCount === 0;
// 如果需要自动隐藏返回空div // 如果需要自动隐藏返回空div
if (shouldAutoHide) { if (shouldAutoHide) {
@@ -1642,95 +1639,55 @@ class XiaoshiTodoButton extends LitElement {
// 渲染按钮 // 渲染按钮
let buttonHtml; let buttonHtml;
if (!this._dataLoaded) {
if (badgeMode) { // 数据加载完成后
// 角标模式只显示图标数量为0时不显示角标 if (badgeMode) {
buttonHtml = html` // 角标模式:只显示图标,根据数量显示角标
<div class="todo-status badge-mode" style="--bg-color: ${buttonBgColor};" @click=${this._handleButtonClick}> const hasWarning = totalIncompleteCount > 0;
<ha-icon class="status-icon" icon="${buttonIcon}"></ha-icon> buttonHtml = html`
</div> <div class="todo-status badge-mode ${hasWarning ? 'has-warning' : ''}" style="--bg-color: ${buttonBgColor};" @click=${this._handleButtonClick}>
`; <ha-icon class="status-icon" icon="${buttonIcon}"></ha-icon>
} else { ${hasWarning ? html`<div class="badge-number">${totalIncompleteCount}</div>` : ''}
// 普通模式 </div>
// 应用锁定白色功能 `;
const iconColor = lockWhiteFg ? 'rgb(255, 255, 255)' : fgColor;
const textColor = lockWhiteFg ? 'rgb(255, 255, 255)' : fgColor;
// 构建显示文本
let displayText = buttonText;
// 根据hide_colon参数决定是否显示冒号
if (!hideColon) {
displayText += ':';
} else {
displayText += ' ';
}
// 根据hide_zero参数决定是否显示0值
if (!hideZero) {
displayText += ' 0';
} else {
// 隐藏0值时使用CSS空格占位符保持布局稳定
displayText += '\u2002'; // 两个en空格大约等于数字"0"的宽度
}
buttonHtml = html`
<div class="todo-status" style="--fg-color: ${textColor}; --bg-color: ${buttonBgColor};" @click=${this._handleButtonClick}>
${!hideIcon ? html`<ha-icon class="status-icon" style="color: ${iconColor};" icon="${buttonIcon}"></ha-icon>` : ''}
${displayText}
</div>
`;
}
} else { } else {
// 数据加载完成后 // 普通模式:显示文本和数量
if (badgeMode) { // 应用锁定白色功能,但预警颜色(红色)不受影响
// 角标模式:只显示图标,根据数量显示角标 let textColor, iconColor;
const hasWarning = totalIncompleteCount > 0; if (totalIncompleteCount === 0) {
buttonHtml = html` // 非预警状态:根据锁定白色设置决定颜色
<div class="todo-status badge-mode ${hasWarning ? 'has-warning' : ''}" style="--bg-color: ${buttonBgColor};" @click=${this._handleButtonClick}> textColor = lockWhiteFg ? 'rgb(255, 255, 255)' : fgColor;
<ha-icon class="status-icon" icon="${buttonIcon}"></ha-icon> iconColor = lockWhiteFg ? 'rgb(255, 255, 255)' : fgColor;
${hasWarning ? html`<div class="badge-number">${totalIncompleteCount}</div>` : ''}
</div>
`;
} else { } else {
// 普通模式:显示文本和数量 // 预警状态:始终使用红色,不受锁定白色影响
// 应用锁定白色功能,但预警颜色(红色)不受影响 textColor = 'rgb(255, 0, 0)';
let textColor, iconColor; iconColor = lockWhiteFg ? 'rgb(255, 255, 255)' : fgColor;
if (totalIncompleteCount === 0) {
// 非预警状态:根据锁定白色设置决定颜色
textColor = lockWhiteFg ? 'rgb(255, 255, 255)' : fgColor;
iconColor = lockWhiteFg ? 'rgb(255, 255, 255)' : fgColor;
} else {
// 预警状态:始终使用红色,不受锁定白色影响
textColor = 'rgb(255, 0, 0)';
iconColor = lockWhiteFg ? 'rgb(255, 255, 255)' : fgColor;
}
// 构建显示文本
let displayText = buttonText;
// 根据hide_colon参数决定是否显示冒号
if (!hideColon) {
displayText += ':';
} else {
displayText += ' ';
}
// 根据hide_zero参数和实际数量决定是否显示数量
if (hideZero && totalIncompleteCount === 0) {
// 隐藏0值时使用CSS空格占位符保持布局稳定
displayText += '\u2002'; // 两个en空格大约等于数字"0"的宽度
} else {
displayText += ` ${totalIncompleteCount}`;
}
buttonHtml = html`
<div class="todo-status" style="--fg-color: ${textColor}; --bg-color: ${buttonBgColor};" @click=${this._handleButtonClick}>
${!hideIcon ? html`<ha-icon class="status-icon" style="color: ${iconColor};" icon="${buttonIcon}"></ha-icon>` : ''}
${displayText}
</div>
`;
} }
// 构建显示文本
let displayText = buttonText;
// 根据hide_colon参数决定是否显示冒号
if (!hideColon) {
displayText += ':';
} else {
displayText += ' ';
}
// 根据hide_zero参数和实际数量决定是否显示数量
if (hideZero && totalIncompleteCount === 0) {
// 隐藏0值时使用CSS空格占位符保持布局稳定
displayText += '\u2002'; // 两个en空格大约等于数字"0"的宽度
} else {
displayText += ` ${totalIncompleteCount}`;
}
buttonHtml = html`
<div class="todo-status" style="--fg-color: ${textColor}; --bg-color: ${buttonBgColor};" @click=${this._handleButtonClick}>
${!hideIcon ? html`<ha-icon class="status-icon" style="color: ${iconColor};" icon="${buttonIcon}"></ha-icon>` : ''}
${displayText}
</div>
`;
} }
// 返回最终的渲染结果(包括按钮和预览卡片) // 返回最终的渲染结果(包括按钮和预览卡片)