Skip to content

Track MyApplication property inside app designer rather than through project system - #4714

Merged
davidwengier merged 9 commits into
dotnet:masterfrom
davidwengier:MyApplicationChanges
May 22, 2019
Merged

davidwengier merged 9 commits into
dotnet:masterfrom
davidwengier:MyApplicationChanges

Conversation

@davidwengier

@davidwengier davidwengier commented Apr 10, 2019 •

Copy link
Copy Markdown
Member

Fixes #4708, part of #4249.

The Situation:
VB projects have an application framework they can opt in to which is enabled by the property pages through the MyApplication property of the BrowseObject. This object has 8 properties and stores its data in a .myapp file under the My Project folder. This file is then used to drive a generator that generates source code that is included in the users project.

Currently the legacy implementation of MyApplication simply creates an instance of our MyApplicationManager, initializes it, and returns the result of calling GetProperties(). It then calls Save when the project file saves, and Close when the project closes.

If the MyApplication property returns a value (ie, the system is in place) it also enables the bottom half of the VB win forms property page which contains a bunch of extra controls most of which work.

The Problem:
CPS currently doesn't have any way (that I can see) to have a property on the BrowseObject that is not reduced to a string at some point.

The Solution:
Since we essentially control everything around MyApplication anyway, my solution is to take full control over its lifetime without worrying about the project system in use at all. This approach has been discussed with David Kean and unless I misunderstood something, is what we discussed. Essentially we have a class that tracks the lifetime of projects, and supplies the appropriate properties object for each when requested, saves when the project is saved, and closes when the project closes. The trackers lifetime is global (its a static/Shared) but it's only initialized when first used. It's used by both legacy and CPS project systems so is independent of both.

At the moment this has been done in a way that only works in legacy (we opt out with a capability) but once the rest of the features of the property pages work we would remove this capability and this would just work by itself as a feature of the property pages. The legacy implementation of the MyApplication property could also be removed since it no longer works with this PR.

The Issues:
It is a fair assumption to make that we're the only consumers of these property pages (managed and legacy), and certainly from a search on GitHub and //index it seems that nobody calls the MyApplication property, this essentially breaks the contract of being a project system because we don't implement the VBProjectProperties3 interface (not that we ever did). Even the example in the code comments wouldn't work (not that it ever would for CPS).

Since we own the generator and all associated code for this an alternate approach would be to just store the properties on the MyApplicationProperties class in the project file and have the generator honour them. There are only 8, they're all strings (or convertible to strings), and I assume this is not an actively developed area of the product so are unlikely to change. This would mean having separate property pages for us and legacy though, so might be not ideal, though we'd use inheritance to maximise the code reuse.

@davidwengier
davidwengier requested a review from a team as a code owner April 10, 2019 06:30
'We only want to save the project file itself, not any other its children (any of the other files in the project)
hr = rdt.SaveDocuments(CUInt(__VSRDTSAVEOPTIONS.RDTSAVEOPT_SaveIfDirty Or __VSRDTSAVEOPTIONS.RDTSAVEOPT_SaveNoChildren),
'but we do want to force it so that the MyApplication file will be saved is necessary
hr = rdt.SaveDocuments(CUInt(__VSRDTSAVEOPTIONS.RDTSAVEOPT_ForceSave Or __VSRDTSAVEOPTIONS.RDTSAVEOPT_SaveNoChildren),

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This actually fixes a bug in the current system, when changing something on a property page that changes the .myapp file and not the project file, we mark the properties tab as dirty but when save is pressed the SaveIfDirty flag means it doesn't actually save, so the .myapp file doesn't save either. If the user then closes VS they get asked to save it, but from experience I can tell you that when VS asks you to save a file that you didn't manually change, you get used to clicking "Don't Save", and in this case you would lose changes. Forcing a save here means at least the running document table events will fire.

Public Overrides Function WriteUserDefinedProperty(PropertyName As String, Value As Object) As Boolean
If PropertyName = Const_EnableVisualStyles Then
If PropertyName = Const_MyApplication Then
Dim x = MyApplicationProperties

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is probably unnecessary, but its a lazy loading property so just in case? ¯\_(ツ)_/¯

Managed;
ClassDesigner;" />
ClassDesigner;
PreventAutomaticMyApplication;" />

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is what opts us out of the new system, so we don't break everyones existing SDK-style VB projects by showing them property page controls that may not work 100%

Dim cookie = GetProjectFileCookie(projectHierarchy)

' Because GetOrAdd always creates a new value, even if using a factory, we just create an object and only
' initialize it later, relying on the face that MyApplicationProperties will not initialize itself twice

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

spelling: face => fact

Private Function TrackInternal(projectHierarchy As IVsHierarchy) As MyApplicationProperties
Dim cookie = GetProjectFileCookie(projectHierarchy)

' Because GetOrAdd always creates a new value, even if using a factory, we just create an object and only

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you clarify what you mean here? I believe the func-accepting overload avoids constructing NewApplicationProperties when the key exists in the collection. It can still create redundant objects in the unlikely case where the optimistic algorithm fails to commit, so cannot be used in cases where you need to guarantee the value is only created once. But in this case I think it's fine.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could have sworn I looked at this, and saw that valueFactory was always called, but looking now you're right it isn't called if the key exists. I guess I could change this to use a Func then, but I'd still want to keep the Init call out of it just in case, as that does a bunch of interop stuff and calls into the hierarchy that its worth avoiding IMO. I could also change this to use a Lazy.. too many options 😛

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed this to a lambda, and corrected the comment.

@jmarolf jmarolf left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🕐

@jmarolf jmarolf left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:shipit:

@davidwengier
davidwengier requested a review from davkean April 23, 2019 03:39
@davidwengier
davidwengier merged commit 39834f7 into dotnet:master May 22, 2019
@davidwengier
davidwengier deleted the MyApplicationChanges branch July 24, 2019 01:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support MyApplication property for VB projects

4 participants