-
Notifications
You must be signed in to change notification settings - Fork 963
Use custom attack ratio slider for consistent cross-browser rendering #3422
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ae2b4fa
Use custom attack ratio slider for consistent cross-browser rendering
hkio120 5599737
Add keyboard accessibility to custom attack ratio slider
hkio120 3af40fc
Keep slider PR focused on visual consistency only
hkio120 5071d81
refactor: extract attack ratio slider into Lit element
hkio120 e900e81
refactor: reuse FluentSlider for attack ratio control
hkio120 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,7 @@ import { EventBus } from "../../../core/EventBus"; | |
| import { Gold } from "../../../core/game/Game"; | ||
| import { GameView } from "../../../core/game/GameView"; | ||
| import { ClientID } from "../../../core/Schemas"; | ||
| import "../../components/FluentSlider"; | ||
| import { AttackRatioEvent } from "../../InputHandler"; | ||
| import { renderNumber, renderTroops } from "../../Utils"; | ||
| import { UIState } from "../UIState"; | ||
|
|
@@ -69,8 +70,7 @@ export class ControlPanel extends LitElement implements Layer { | |
| newAttackRatio = 0.1; | ||
| } | ||
|
|
||
| this.attackRatio = newAttackRatio; | ||
| this.onAttackRatioChange(this.attackRatio); | ||
| this.setAttackRatio(newAttackRatio); | ||
| }); | ||
| } | ||
|
|
||
|
|
@@ -124,15 +124,36 @@ export class ControlPanel extends LitElement implements Layer { | |
| this.requestUpdate(); | ||
| } | ||
|
|
||
| private handleRatioSliderInput(e: Event) { | ||
| const input = e.target as HTMLInputElement; | ||
| const value = Number(input.value); | ||
| this.attackRatio = value / 100; | ||
| private setAttackRatio(newRatio: number) { | ||
| const clamped = Math.min(1, Math.max(0.01, newRatio)); | ||
| this.attackRatio = clamped; | ||
| localStorage.setItem("settings.attackRatio", String(this.attackRatio)); | ||
| this.onAttackRatioChange(this.attackRatio); | ||
| this.requestUpdate(); | ||
| } | ||
|
|
||
| private handleRatioSliderPointerUp(e: Event) { | ||
| (e.target as HTMLInputElement).blur(); | ||
| private handleAttackRatioSliderChange = ( | ||
| e: CustomEvent<{ value: number }>, | ||
| ) => { | ||
| this.setAttackRatio(e.detail.value); | ||
| }; | ||
| private renderCustomAttackRatioSlider(compact: boolean = false) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you make the slider its own lit element? Then we can reuse it for other things
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done is it good ? |
||
| const ratioPercent = Math.max(1, Math.min(100, this.attackRatio * 100)); | ||
|
|
||
| return html` | ||
| <fluent-slider | ||
| .value=${this.attackRatio} | ||
| .min=${0.01} | ||
| .max=${1} | ||
| .step=${0.01} | ||
| .compact=${compact} | ||
| .inline=${true} | ||
| .live=${true} | ||
| .ariaLabel=${"Attack ratio"} | ||
| .ariaValueText=${`${Math.round(ratioPercent)}%`} | ||
| @value-changed=${this.handleAttackRatioSliderChange} | ||
| ></fluent-slider> | ||
| `; | ||
| } | ||
|
|
||
| private calculateTroopBar(): { greenPercent: number; orangePercent: number } { | ||
|
|
@@ -319,15 +340,7 @@ export class ControlPanel extends LitElement implements Layer { | |
| )})</span | ||
| > | ||
| </div> | ||
| <input | ||
| type="range" | ||
| min="1" | ||
| max="100" | ||
| .value=${String(Math.round(this.attackRatio * 100))} | ||
| @input=${(e: Event) => this.handleRatioSliderInput(e)} | ||
| @pointerup=${(e: Event) => this.handleRatioSliderPointerUp(e)} | ||
| class="flex-1 h-2 accent-blue-500 cursor-pointer" | ||
| /> | ||
| <div class="flex-1">${this.renderCustomAttackRatioSlider()}</div> | ||
| </div> | ||
| `; | ||
| } | ||
|
|
@@ -366,15 +379,7 @@ export class ControlPanel extends LitElement implements Layer { | |
| </div> | ||
| <!-- Attack ratio slider --> | ||
| <div class="flex-1" translate="no"> | ||
| <input | ||
| type="range" | ||
| min="1" | ||
| max="100" | ||
| .value=${String(Math.round(this.attackRatio * 100))} | ||
| @input=${(e: Event) => this.handleRatioSliderInput(e)} | ||
| @pointerup=${(e: Event) => this.handleRatioSliderPointerUp(e)} | ||
| class="w-full h-1.5 accent-blue-500 cursor-pointer" | ||
| /> | ||
| ${this.renderCustomAttackRatioSlider(true)} | ||
| </div> | ||
| </div> | ||
| `; | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Guard
setAttackRatioagainst non-finite input and redundant writes.Line 128 clamps range, but
NaNstill passes through asNaN, then Line 130 persists"NaN"and UI math can break. Also, unchanged values still trigger storage/event/update churn.Suggested hardening
private setAttackRatio(newRatio: number) { - const clamped = Math.min(1, Math.max(0.01, newRatio)); - this.attackRatio = clamped; - localStorage.setItem("settings.attackRatio", String(this.attackRatio)); - this.onAttackRatioChange(this.attackRatio); - this.requestUpdate(); + const safeRatio = Number.isFinite(newRatio) ? newRatio : 0.2; + const clamped = Math.min(1, Math.max(0.01, safeRatio)); + if (clamped === this.attackRatio) return; + this.attackRatio = clamped; + localStorage.setItem("settings.attackRatio", String(clamped)); + this.onAttackRatioChange(clamped); + this.requestUpdate(); }🤖 Prompt for AI Agents