Skip to content

Commit

Permalink
feat: add "isTrusted" property to Slider "change" event (#11717)
Browse files Browse the repository at this point in the history
Fixes TIMOB-27977
  • Loading branch information
m1ga authored Jul 1, 2020
1 parent d2db8cf commit 8e96445
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ public void propertyChanged(String key, Object oldValue, Object newValue, KrollP
pos = TiConvert.toFloat(newValue);
int curPos = (int) Math.floor(scaleFactor * (pos + offset));
seekBar.setProgress(curPos);
onProgressChanged(seekBar, curPos, true);
onProgressChanged(seekBar, curPos, false);
} else if (key.equals("min")) {
min = TiConvert.toInt(newValue);
minRange = min;
Expand All @@ -253,7 +253,7 @@ public void propertyChanged(String key, Object oldValue, Object newValue, KrollP
}
updateControl();
int curPos = (int) Math.floor(scaleFactor * (pos + offset));
onProgressChanged(seekBar, curPos, true);
onProgressChanged(seekBar, curPos, false);
} else if (key.equals("minRange")) {
minRange = TiConvert.toInt(newValue);
updateRange();
Expand All @@ -262,7 +262,7 @@ public void propertyChanged(String key, Object oldValue, Object newValue, KrollP
}
updateControl();
int curPos = (int) Math.floor(scaleFactor * (pos + offset));
onProgressChanged(seekBar, curPos, true);
onProgressChanged(seekBar, curPos, false);
} else if (key.equals("max")) {
max = TiConvert.toInt(newValue);
maxRange = max;
Expand All @@ -272,7 +272,7 @@ public void propertyChanged(String key, Object oldValue, Object newValue, KrollP
}
updateControl();
int curPos = (int) Math.floor(scaleFactor * (pos + offset));
onProgressChanged(seekBar, curPos, true);
onProgressChanged(seekBar, curPos, false);
} else if (key.equals("maxRange")) {
maxRange = TiConvert.toInt(newValue);
updateRange();
Expand All @@ -281,7 +281,7 @@ public void propertyChanged(String key, Object oldValue, Object newValue, KrollP
}
updateControl();
int curPos = (int) Math.floor(scaleFactor * (pos + offset));
onProgressChanged(seekBar, curPos, true);
onProgressChanged(seekBar, curPos, false);
} else if (key.equals(TiC.PROPERTY_TINT_COLOR)) {
String stringValue = TiConvert.toString(newValue);
if (stringValue != null) {
Expand Down Expand Up @@ -356,6 +356,7 @@ public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
data.put(TiC.PROPERTY_VALUE, scaledValue);
data.put(TiC.EVENT_PROPERTY_THUMB_OFFSET, offset);
data.put(TiC.EVENT_PROPERTY_THUMB_SIZE, size);
data.put("isTrusted", fromUser);
proxy.setProperty(TiC.PROPERTY_VALUE, scaledValue);

fireEvent(TiC.EVENT_CHANGE, data);
Expand Down
5 changes: 5 additions & 0 deletions apidoc/Titanium/UI/Slider.yml
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,11 @@ events:
type: Point
platforms: [android]

- name: isTrusted
summary: True if change was made by the user. False if change was made programmatically.
type: Boolean
since: 9.1.0

- name: click
summary: Fired when the device detects a click against the view.
description: |
Expand Down
9 changes: 7 additions & 2 deletions iphone/Classes/TiUISlider.m
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ - (void)_setValue:(id)value
[[self sliderView] setValue:newValue animated:[TiUtils boolValue:@"animated" properties:properties def:NO]];
}

[self sliderChanged:[self sliderView]];
[self sliderChanged:[self sliderView] isTrusted:NO];
}

- (void)setEnabled_:(id)value
Expand Down Expand Up @@ -275,12 +275,17 @@ - (CGFloat)verifyHeight:(CGFloat)suggestedHeight
#pragma mark Delegates

- (void)sliderChanged:(id)sender
{
[self sliderChanged:sender isTrusted:YES];
}

- (void)sliderChanged:(id)sender isTrusted:(BOOL)isTrusted
{
NSNumber *newValue = [NSNumber numberWithFloat:[(UISlider *)sender value]];
[self.proxy replaceValue:newValue forKey:@"value" notification:NO];

if ([self.proxy _hasListeners:@"change"]) {
[self.proxy fireEvent:@"change" withObject:[NSDictionary dictionaryWithObject:newValue forKey:@"value"]];
[self.proxy fireEvent:@"change" withObject:[NSDictionary dictionaryWithObjectsAndKeys:newValue, @"value", NUMBOOL(isTrusted), @"isTrusted", nil]];
}
}

Expand Down
61 changes: 61 additions & 0 deletions tests/Resources/ti.ui.slider.addontest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Appcelerator Titanium Mobile
* Copyright (c) 2015-Present by Axway, Inc. All Rights Reserved.
* Licensed under the terms of the Apache Public License
* Please see the LICENSE included with this distribution for details.
*/
/* eslint-env mocha */
/* eslint no-unused-expressions: "off" */
'use strict';
const should = require('./utilities/assertions');

describe('Titanium.UI.Slider', function () {
let win;

this.timeout(5000);

afterEach((done) => {
if (win) {
let t = setTimeout(function () {
if (win) {
win = null;
done();
}
}, 3000);
win.addEventListener('close', function listener () {
clearTimeout(t);
if (win) {
win.removeEventListener('close', listener);
}
win = null;
done();
});
win.close();
} else {
win = null;
done();
}
});

it('change event', (finish) => {
win = Ti.UI.createWindow();
const slider = Ti.UI.createSlider({ min: 0, max: 100, value: 50 });
win.add(slider);
win.addEventListener('open', () => {
slider.addEventListener('change', (e) => {
try {
should(e.value).be.a.Number();
should(e.value).be.eql(75);
should(e.isTrusted).be.a.Boolean();
should(e.isTrusted).be.false();
should(e.source).be.eql(slider);
finish();
} catch (err) {
finish(err);
}
});
slider.value = 75;
});
win.open();
});
});

0 comments on commit 8e96445

Please sign in to comment.