Skip to content

Commit 61e617f

Browse files
committed
mcomponents mousewheel optimizations
1 parent bc20321 commit 61e617f

15 files changed

Lines changed: 78 additions & 68 deletions

modules/components/src/components.module.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@ import {
77
import {
88
KnobComponent
99
} from './components/knob/knob.component'
10-
import {
11-
MouseWheelDirective
12-
} from './directives/mousewheel.directive'
13-
1410
import {
1511
ValueScreenComponent
1612
} from './components/value-screen/value-screen.component'
@@ -58,7 +54,6 @@ import { ColorsService } from './services/colors.service'
5854
declarations: [
5955
ClickedOutsideDirective,
6056
KnobComponent,
61-
MouseWheelDirective,
6257
ValueScreenComponent,
6358
ButtonComponent,
6459
ToggleComponent,

modules/components/src/components/flat-slider/flat-slider.component.html

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11

2-
<div class="container" #container [ngStyle]="containerStyle">
2+
<div class="container" #container
3+
[ngStyle]="containerStyle"
4+
(mousewheel)="mouseWheel($event)"
5+
(mousedown)="mousedown($event)"
6+
(mouseup)="mouseup($event)"
7+
(mousemove)="mousemove($event)"
8+
(mouseenter)="mouseenter()"
9+
(mouseleave)="mouseleave()"
10+
>
311
<div class="groove" [ngStyle]="grooveStyle"></div>
412
<div class="groove-filling" [ngStyle]="grooveFillingStyle"></div>
513
<div class="thumbNotch" *ngIf="showMiddleNotch" [ngStyle]="thumbNotchStyle"></div>

modules/components/src/components/flat-slider/flat-slider.component.ts

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import {
44
Input,
55
Output,
66
EventEmitter,
7-
HostListener,
87
ElementRef,
9-
HostBinding
8+
HostBinding,
9+
HostListener
1010
} from '@angular/core'
1111
import {
1212
UtilitiesService
@@ -28,7 +28,7 @@ export interface FlatSliderValueChangedEvent {
2828
export class FlatSliderComponent {
2929
constructor (
3030
public utils: UtilitiesService,
31-
public elem: ElementRef,
31+
public elem: ElementRef<HTMLElement>,
3232
public sanitizer: DomSanitizer
3333
) {}
3434

@@ -47,6 +47,7 @@ export class FlatSliderComponent {
4747
@Input() notches: number[]
4848
@Input() thumbRadius = 4
4949
@Input() thumbBorderSize = 1
50+
@Input() wheelAnimationFPS = 30
5051

5152
@Output() stickedToMiddle = new EventEmitter()
5253
@ViewChild('container', { static: true }) containerRef!: ElementRef
@@ -141,10 +142,14 @@ export class FlatSliderComponent {
141142
return value
142143
}
143144

145+
private lastWheelEvent = new Date().getTime()
146+
private get wheelDebouncer () { return 1000 / this.wheelAnimationFPS }
144147
@HostListener('mousewheel', [ '$event' ])
145148
mouseWheel (event: WheelEvent) {
146149
if (this.enabled && this.scrollEnabled) {
147-
// const multiplier = (this.max - this.min) / 1000
150+
const now = new Date().getTime()
151+
if (now - this.lastWheelEvent < this.wheelDebouncer) return
152+
this.lastWheelEvent = now
148153
let progress = this.progress
149154
progress += -event.deltaY / 1000
150155
if (progress < 0) progress = 0
@@ -187,7 +192,6 @@ export class FlatSliderComponent {
187192
return value
188193
}
189194

190-
@HostListener('mousedown', [ '$event' ])
191195
mousedown (event: MouseEvent) {
192196
if (this.enabled) {
193197
this.dragging = true
@@ -196,7 +200,6 @@ export class FlatSliderComponent {
196200
}
197201
}
198202

199-
@HostListener('mousemove', [ '$event' ])
200203
mousemove (event: MouseEvent) {
201204
if (this.enabled && this.dragging) {
202205
this.value = this.getValueFromMouseEvent(event)
@@ -205,13 +208,11 @@ export class FlatSliderComponent {
205208
}
206209

207210
public mouseInside = false
208-
@HostListener('mouseenter')
209-
onMouseEnter (): void {
211+
mouseenter (): void {
210212
this.mouseInside = true
211213
}
212214

213-
@HostListener('mouseleave')
214-
onMouseLeave (): void {
215+
mouseleave (): void {
215216
this.mouseInside = false
216217
this.dragging = false
217218
}
@@ -246,11 +247,6 @@ export class FlatSliderComponent {
246247
}
247248
}
248249

249-
@HostListener('mouseup', [ '$event' ])
250-
onMouseUp (event: MouseEvent) {
251-
this.dragging = false
252-
}
253-
254250
mouseup (event: MouseEvent) {
255251
this.dragging = false
256252
}

modules/components/src/components/icon/icons.ts

Lines changed: 16 additions & 2 deletions
Large diffs are not rendered by default.
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
<eqm-container class="container" #container>
2-
<input [(ngModel)]="text" [placeholder]="placeholder" (input)="inputChanged()" (keyup.enter)="enterPressed()" class="input-field" [readonly]="!enabled || !editable" [style.font-size.px]="fontSize">
2+
<input [type]="type" [(ngModel)]="text"
3+
[placeholder]="placeholder" autocomplete="true"
4+
(input)="inputChanged()" (keyup.enter)="enterPressed()"
5+
class="input-field" [readonly]="!enabled || !editable"
6+
[style.font-size.px]="fontSize">
37
</eqm-container>

modules/components/src/components/input-field/input-field.component.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export class InputFieldComponent implements OnInit {
1313
@Input() editable = true
1414
@HostBinding('class.enabled') @Input() enabled = true
1515
@Input() fontSize = 12
16+
@Input() type: string = 'text'
1617
@ViewChild('container', { static: true }) container!: ElementRef
1718

1819
ngOnInit () {

modules/components/src/components/knob/knob.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<div #container class="container" mouseWheel (mouseWheel)="mouseWheel($event)" (dblclick)="doubleclick()"
1+
<div #container class="container" (dblclick)="doubleclick()"
22
(mousedown)="mousedown($event)" (mousemove)="mousemove($event)" (mouseup)="mouseup($event)">
33

44
<!-- Large -->

modules/components/src/components/knob/knob.component.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import {
77
ViewChild,
88
HostListener,
99
HostBinding,
10-
ElementRef
10+
ElementRef,
11+
ChangeDetectorRef
1112
} from '@angular/core'
1213
import { UtilitiesService } from '../../services/utilities.service'
1314

@@ -85,19 +86,23 @@ export class KnobComponent implements OnInit {
8586
return this.middleValue
8687
}
8788

88-
constructor (public utils: UtilitiesService) {}
89+
constructor (public utils: UtilitiesService, public changeRef: ChangeDetectorRef) {}
8990

9091
async ngOnInit () {
9192
this.container = this.containerRef.nativeElement
9293
}
9394

95+
private lastWheelEvent = new Date().getTime()
96+
private readonly wheelDebouncer = 1000 / 30
97+
@HostListener('mousewheel', [ '$event' ])
9498
mouseWheel (event: WheelEvent) {
9599
if (this.enabled) {
96100
this.continueAnimation = false
97-
const oldValue = this.value
101+
const now = new Date().getTime()
102+
if ((now - this.lastWheelEvent) < this.wheelDebouncer) return
103+
this.lastWheelEvent = now
98104
this.value += -event.deltaY / (1000 / this.max)
99-
const newValue = this.value
100-
if (oldValue !== newValue) this.userChangedValue.emit({ value: this.value })
105+
this.userChangedValue.emit({ value: this.value })
101106
}
102107
}
103108

modules/components/src/components/skeuomorph-slider/skeuomorph-slider.component.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import {
77
HostListener,
88
ViewChild,
99
EventEmitter,
10-
HostBinding
10+
HostBinding,
11+
OnDestroy
1112
} from '@angular/core'
1213
import {
1314
UtilitiesService
@@ -24,7 +25,10 @@ export interface SkeuomorphSliderValueChangedEvent {
2425
styleUrls: [ './skeuomorph-slider.component.scss' ]
2526
})
2627
export class SkeuomorphSliderComponent implements OnInit {
27-
constructor (public utils: UtilitiesService, public elRef: ElementRef) {}
28+
constructor (
29+
public utils: UtilitiesService,
30+
public elRef: ElementRef<HTMLElement>
31+
) {}
2832

2933
@Input() min: number = 0
3034
@Input() max: number = 1
@@ -77,9 +81,14 @@ export class SkeuomorphSliderComponent implements OnInit {
7781

7882
get value () { return this._value }
7983

84+
private lastWheelEvent = new Date().getTime()
85+
private readonly wheelDebouncer = 1000 / 30
8086
@HostListener('mousewheel', [ '$event' ])
8187
onMouseWheel (event: WheelEvent): void {
8288
if (this.enabled && this.scrollEnabled) {
89+
const now = new Date().getTime()
90+
if ((now - this.lastWheelEvent) < this.wheelDebouncer) return
91+
this.lastWheelEvent = now
8392
this.value += -event.deltaY / 100
8493
this.userChangedValue.emit({ value: this.value })
8594
}
@@ -196,6 +205,6 @@ export class SkeuomorphSliderComponent implements OnInit {
196205
}
197206

198207
calculateTop () {
199-
return `${this.utils.mapValue(this._value, this.min, this.max, parseInt(this.elRef.nativeElement.offsetHeight) - 25, 0)}px`
208+
return `${this.utils.mapValue(this._value, this.min, this.max, this.elRef.nativeElement.offsetHeight - 25, 0)}px`
200209
}
201210
}

modules/components/src/components/toggle/toggle.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<div class="container" (click)="toggleState()">
1+
<div class="container {{enabled ? 'enabled' : ''}}" (click)="toggleState()">
22
<div class="indicator left"></div>
33
<div class="indicator right"></div>
44
<div [class]="'switch ' + (state ? 'on' : 'off')"></div>

0 commit comments

Comments
 (0)