Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-17409] Both Ti.UI.View.rect and event coordinates should use the (same) documented unit

GitHub Issuen/a
TypeBug
PriorityMedium
StatusOpen
ResolutionUnresolved
Affected Version/sRelease 3.3.0
Fix Version/sn/a
ComponentsAndroid, iOS
LabelsTCSupport, click, rect, unit
ReporterFokke Zandbergen
AssigneeUnknown
Created2014-07-10T09:00:35.000+0000
Updated2018-02-28T20:04:21.000+0000

Description

While making the [nl.fokkezb.color](https://github.com/fokkezb/nl.fokkezb.color) widget I noticed Ti.UI.View.rect and the x and y coordinate given in click (and other) events use different units. The documentation of [rect](http://docs.appcelerator.com/titanium/latest/#!/api/Titanium.UI.View-property-rect) clearly states it always uses the system unit (px on Android, dp on iOS) while the click docs (http://docs.appcelerator.com/titanium/latest/#!/api/Titanium.UI.View-event-click) don't specify the unit. When you execute the following app.js without and with ti.ui.defaultunit set to both dp and px on both platforms and click in the bottom right corner of the screen you'll see rect does *not* use the system unit but rather the defaultunit. The event coordinates *do* always use the system unit. The results can be found under the code snippet. The documentation should be fixed, but I'd also request for both to use the same units, be it system or default unit.
console.debug(Ti.App.Properties.getString('ti.ui.defaultunit'));
var win = Ti.UI.createWindow({backgroundColor:'white'});
win.addEventListener('postlayout', function(e) {
    console.debug('rect: ' + win.rect.width + ',' + win.rect.height);
});
win.addEventListener('click', function(e) {
    console.debug('click: ' + e.x + ',' + e.y);
});
win.open();

iOS

||ti.ui.defaultunit||rect||click|| |(none)|dp|dp| |px|px|dp| |dp|dp|dp|

Android

||ti.ui.defaultunit||rect||click|| |(none)|px|px| |px|px|px| |dp|dp|px|

Comments

  1. Mauro Parra-Miranda 2014-07-28

    Thanks for your report! The DOC team will discuss this and set the priority.
  2. Ingo Muschenetz 2014-08-14

    3.4.0 is moved forward, and 3.5.0 is taking its place in the calendar.
  3. Jason Priebe 2014-09-17

    I agree that this is a problem. I always set my defaultunit to "dip" for consistency across platforms. I was very surprised to find my event coordinates in pixels on android. Now I have to write a silly little special case to convert those px values to dp for android, and if you *do* fix this behavior, I will have to remove my special case. I think that in the absence of a ti.ui.defaultunit setting, all positions and dimensions should be reported in the system unit. If the ti.ui.defaultunit setting is present, all positions and dimensions should be reported in the default unit. I would further urge that you change Studio so that it sets ti.ui.defaultunit="dip" for all new projects. I think that for beginners with Titanium, the px/dp issue makes it slightly harder to build cross-platform apps with consistent UIs. It especially hurts those developers who build their entire app for iOS first, and then think they'll have an easy time getting it to run on android (bad idea on a lot of levels, but very common). Then those users are even more frustrated than they need to be, and they go around writing nasty blog posts about Titanium.
  4. Anna 2016-04-21

    @Jason Priebe I agree with you. I'm having a lot of problems because it. For me is a very big problem I'm loosing a lot of time solving it. And I'm still having problems now.

JSON Source